When a binary (C/C++) is executed under Linux,
- How is the stack initialized for the process?
- How does the stack grow and up to what limit?
- Using
ulimit
, I can have a limit number and by usingsetrlimit
, I can modify it, but up to what limit, how can I determine it? - Is the same stack size allocated for all executing processes?
As you can see in the code below, I have recursively called func() for push operation only, and the stack grew up to around approximately 8 MB. And it crashed (stack overflow!).
void func()
{
static int i=0;
int arr[1024]={0};
printf("%d KB pushed on stack!\n",++i*sizeof(int));
func();
}
int main()
{
func();
return 0;
}
output snippet:
8108 KB pushed on stack!
8112 KB pushed on stack!
8116 KB pushed on stack!
8120 KB pushed on stack!
Segmentation fault (core dumped)
- Where did these approximately 8 MB come from?