Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.