M BUZZ CRAZE NEWS
// news

Modification of stack size by ulimit -s

By John Parsons

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?

1 Answer

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy