Modification of stack size by ulimit -s
When I type ulimit -s
in terminal it shows 8192
. Does it mean that in my C code I could only have local variable of 8192 bytes?
I am confused which stack size. Does ulimit -s
change when we modify its value?
Solution 1:
First of all, it is 8192 kilo bytes, and not bytes. Furthermore stack is one thing, variable is another, and heap yet another. See this explanation of differences between stack and heap, for example, or this page. As far as I know stack is used for local and short lived variables, and it depends on the compiler whether stack or heap are used.
As far as I can tell, if you use [mc]alloc
and friends to allocate memory, you are not touching upon the stack, and the limits do not hold.
But yes, trying the following at ulimit -s
equal to 8192 will result in a segmentation fault:
#include <stdlib.h>
int main() {
char foo[10000000] ;
foo[0] = 'a' ;
exit( 0 ) ;
}
Here, the variable foo
is too large.
$ gcc test.c
$ ./a.out
Segmentation fault (core dumped)
However, if you change the ulimit (e.g. ulimit -s 16000
), it will work.