1

my problem is as follows:

This code returns a seg fault (core dumped):

#include <stdio.h>

int main(void) {
  double array[128][128][128];

  printf("done");

  return 1;
}

while this code is ok:

#include <stdio.h>
double array[128][128][128];

int main(void) {

  printf("done");

  return 1;
}

Of course is a problem of memory, because if I put inside the main function the declaration:

float array[127][128][128];

the code works well. On the other hand, if I use "malloc" for allocating the cube inside the main function, the code works well also. I can not understand the reason for that. There is a simple explanation?

Thanks

Pablo
  • 2,443
  • 1
  • 20
  • 32

1 Answers1

1

Yes you are correct this is because when you are putting double array[128][128][128]; inside the main then it is allocating more memory bytes on the stack which is not supported by your OS.

But when it allocated outside the main then it gets the memory from outside the application pool and hence you dont see any error.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I doubt that it's the OS, but rather the compiler settings. There is probably a compiler (or linker) switch to allow a larger stack. – David Conrad Dec 18 '13 at 20:21