sigvardsen
sigvardsen

Reputation: 1531

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?

Upvotes: 0

Views: 89

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

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;

Upvotes: 2

mikedu95
mikedu95

Reputation: 1736

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.

Upvotes: 0

Related Questions