-1

I was just going through this Wikipedia entry. Out of curiosity to find the stack size allocated to a simple process, i tried this

int main()
{
    static int count = 0;
    cout<<"  Count = "<<count++<<endl;
    main();
    return 0;
}

Compiler DevC++ I got this :- enter image description here

Till this point everything is fine, understandable, by the way from last digit i.e. 43385 can i guess the maximum stack size - on 32 bit machine (what if i say 4 bytes(4 bytes for return address on stack for each call), i may sound silly on this.

Now if i modify my program to :-

void foo()
{
    static int count = 0;
    cout<<"  Count = "<<count++<<endl;
    foo();
}

int main()
{
    foo();
    return 0;
}

In this i get Stack Over flow at count :- 130156 (ok, fine)

But my question if, i add one function in between main and foo, i get this count decrements by 1(130155), if 2 functions in b/w foo and main count is decremented by 2(130154) and so on. Why is this behavior? Is it because 1 space is being consumed for each function address.

instance
  • 1,366
  • 15
  • 23

1 Answers1

1

Firstly correct your program by adding Count++ (silly). Stack size is not fixed, most compilers let you specify the Stack size. Stack size is also dependent on some factors like Platform, toolchain, ulimit,and other parameters.There are many static and dynamic properties that can influence it. There are three kinds of memory limits: for 32-bit (windows) Static data - 2GB Dynamic data - 2GB Stack data - 1GB (the stack size is set by the linker, the default is 1MB. This can be increased using the Linker property System > Stack Reserve Size).

by using your program you can guess the current stack size. The memory-limits-applications-windows Stack Overflow Recursion in C c-maximum-stack-size-of-program will help you.

Community
  • 1
  • 1
Anurag Singh
  • 492
  • 6
  • 16
  • ohhh may bad, corrected, i din copy-pasted the code just wrote it like that only, so missed it. Anyway thanx for your reply and useful link. – instance Aug 30 '15 at 08:09