Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the stack size be changed dynamically - How?

Tags:

c

stack

Can the stack size be changed dynamically in C?

If yes, How?

like image 704
aks Avatar asked Feb 12 '10 09:02

aks


3 Answers

It depends on OS you are using.

On Unix/Linux you can use POSIX syscall setrlimit() for RLIMIT_STACK resource.

See man setrlimit for details.

like image 84
qrdl Avatar answered Nov 13 '22 23:11

qrdl


By dynamically do you mean changing the stack size while the code is in execution? AFAIK, that cannot be done. But, you can set the stack size before you run your application. You can do this by using the "ulimit -s" command in linux which will set the stack size for all process executed under that shell.

Incase of windows, the same can be done in VC6 for that project by setting the stack size in Project Properties->link options->output->stack allocations->reserve. I am not aware for VC8, but such options might be available.

like image 2
Jay Avatar answered Nov 13 '22 22:11

Jay


In a single-threaded program under Linux, the stack will grow automatically until it crashes into something else in the memory space. This is usually the heap and on 32-bit systems this means that you can typically have several GB of stack.

In a multi-threaded program, this isn't generally possible as another thread's stack will be in the way.

You can control the stack size when creating a new thread, but this is generally a bad idea as it is architecture-dependent how much stack is required by a task.

It's pretty low level stuff and mostly controlled by your C library / threading library. Mess around at your peril :)

like image 2
MarkR Avatar answered Nov 13 '22 22:11

MarkR