-9

とても簡単な質問があります:

#include < iostream> 
#include < complex>

using namespace std;

int main()
{

    int k=200000;

    complex <double> r0[k/2],r1[k/2]; ///wrong!!!

    complex <float> r0[k/2],r1[k/2];  ///right

    return 0;
}

複雑な配列を作成したいだけです。k=200000ここで、コンピューターが動作を停止します (エラーではなく、ファイルが動作を停止していることを示していますcomplex <double>) complex<float>。その理由は何ですか?

4

1 に答える 1

6

You are "blowing up the stack" - when you have local variable in main, like this, it will use space on the stack. In this case, k * sizeof double or k * sizeof float - since typical compilers use 4 bytes for float, we're talking about 800KB for the second variant, and 1.6MB for the first variant if the stack is only 1MB, then the first one will go way beyond the size of the stack.

There are several solutions, one of which is of course to adopt the proper C++ method of using vector:

vector<complex <double> > r0, r1;

r0.resize(k/2);
r1.resize(k/2);

Two other options

Allocate dynamically:

complex<double> *r0 = new complex<double>[k/2];

...

delete [] r0;

Or make r0 and r1 global variables.

于 2013-03-11T16:39:19.583 に答える