Home Ask Login Register

Developers Planet

Your answer is one click away!

sigvardsen February 2016

Big array not causing stack overflow

I can find plenty of examples of developers complaining that a big array initialized on the stack create a stack overflow error

int main(int argc, const char * argv[])
    int v[100000000];
    memset(&v, 0, sizeof(v));

When compiling on Apple LLVM 7.0, this does not cause a stack overflow, this puzzles me as the array has a size of ~400Mb, significantly more than what is usually the size of the stack.

Why does the above code not cause stack overflow?


iharob February 2016

Since you are not using v then probably the compiler is not allocating it, try something like

int v[100000000];
for (int i = 0 ; i < sizeof(v) / sizeof(*v) ; ++i)
    v[i] = 0;

mikedu95 February 2016

Your array is more than 100 Mb (*) but assuming it is 100 Mb, that means that either your stack size is larger than 100 Mb, or either your compiler ignored it because you do not use it. That's a compiler optimization.

(*) Indeed 1M = 1024 * 1024, not 1000 * 1000. And one int is more than 1 bit, more than 1 Byte too. And finally, Mb means Megabit and MB means Megabyte.

Post Status

Asked in February 2016
Viewed 2,480 times
Voted 8
Answered 2 times


Leave an answer

Quote of the day: live life