-1

I declare this structure:

struct matrix27 {
  double mat27[1083][1083];
} a1;

My program runs with this structure.
But when I declare this structure:

struct matrix27 {
  double mat27[5034][5034];
} a1;

My program won't run (it compiles, but when I run it, windows stop it."filename.exe has stopped working").

I used a1 in several functions, I want to declare it dynamically, how can i do that?

Eran Egozi
  • 775
  • 1
  • 7
  • 18
Jafar
  • 9
  • 2
  • 6
    What do you mean by "I can't run it"? Does it not compile? Does it crash at runtime? What error messages are you seeing? – Oliver Charlesworth Feb 06 '11 at 21:41
  • it compile,but when i run it windows stop it."filename.exe has stopped working". – Jafar Feb 06 '11 at 23:01
  • [I cannot reproduce your problem.](http://codepad.org/3CxgF8mE) Could you provide a complete, compilable test case which still shows the error you don't understand? – Fred Nurk Feb 07 '11 at 00:09
  • @FredN: It reproduces for me using VC9 on Win7/64. – sbi Feb 07 '11 at 03:04
  • @sbi: If you're sure your test case is identical to Jafar's, could you update the question to include it? – Fred Nurk Feb 07 '11 at 03:05
  • @FredN: Given the question, how would I know it's identical? `:)` Anyway, [this](http://codepad.org/OxEf7s32) blows up at runtime when compiled with VC9 using my test project's settings. – sbi Feb 07 '11 at 03:09
  • @sbi: That's my point, we can't know because we don't know what's really happening and need more info from Jafar. :) We don't even know what compiler he's using. Your example works on codepad, for example, but you didn't include the code we are given in the question. – Fred Nurk Feb 07 '11 at 03:10

2 Answers2

4

I suppose 5034^2*sizeof(double) is too big a chunk of memory to fit into your platform's stack space. (It's almost 200MB on my platform and a program trying to allocate so much stack space crashes.) You can circumvent it by putting the memory on the heap.

And make sure you read the C++ array FAQ.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • Given a1 is defined along with the class type, I doubt it's on the stack – at least if the given incomplete code is representative of what's really happening. – Fred Nurk Feb 07 '11 at 00:06
3

sbi is right. If you really want to put this structure on the stack, you'll have to increase your stack size. If you're using Visual Studio, it can be done choosing (project) Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size.

Vladimir
  • 1,781
  • 13
  • 12