0

My main() function is -

int main(int argc, char *argv[]) {
    std::cout << "Hello, World!" << std::endl;
    int real[512][512] = {0};
    int imaginary[512][512] = {0};
    return 0;
}

It gives me segmentation fault but if I comment out one of the 2D arrays it works. I'm working with images and previously 256x256 array worked but I don't know why it's giving that error.

paul-shuvo
  • 1,874
  • 4
  • 33
  • 37
  • Possible duplicate of [Getting a stack overflow exception when declaring a large array](https://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array) – Employed Russian Nov 16 '18 at 05:52

1 Answers1

1

You are probably running out of stack space as the arrays are quite big. (So this is a very appropriate question for stack overflow :-) )

Move the declarations out of the main function, so the arrays are put in normal memory instead.

Look e.g. here for more info:

https://craftofcoding.wordpress.com/2015/12/07/memory-in-c-the-stack-the-heap-and-static/

Sami Sallinen
  • 3,203
  • 12
  • 16