Can the stack siz开发者_如何转开发e be changed dynamically in C?
If yes, How?
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.
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.
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 :)
In general, it's something that can't be done robustly because address space needs to be reserved for the stack. If objects were already allocated on the heap with addresses within the new desired stack-range, you'd be in big trouble. On systems with less memory than address space it could be possible, but I doubt you'll see many systems that allow it. C does not require nor support any such mechanisms for it.
No, this is outside the scope of C.
Why do you need to do this? It depends on the OS and isn't something that C itself gets directly involved in (although specific linkers and runtime environments have varying ways of managing the configuration of such things).
What OS have you got, and what are you trying to achieve?
精彩评论