简体   繁体   中英

Is there a function to set a process stack/heap memory allocation?

I know this is an OS function. But is there a way of increasing your overall stack or heap size through C? sbrk() is used to change the size of data segment right? Does this imply both stack and heap?

You mentioned sbrk() , so I'm going to assume you mean Unix/Linux. sbrk() will change the size of the data segment, but usually the stack is in a different memory space than the data segment, to keep people from overwriting the stack and doing evil things. Typically you'll set your stack size before you start the program running by using ulimit from your shell.

Note: sbrk() is deprecated in favor of malloc() .

The Open Unix specification defines (and Linux implements) the getrlimit() and setrlimit() functions, which also allow you to play with system limits.

Virtual memory OSes (when using a CPU with MMU) automatically grow the data/stack segment when needed, up to a maximum. On POSIX systems the maximums can be configured using setrlimit(), as W. Craig Trader said. POSIX defines RLIMIT_DATA, RLIMIT_STACK and RLIMIT_AS for the limits.

malloc() internally uses brk() to grow/shrink the data segment, or mmap()/munmap(), to request/release memory mappings. The stack is grown when the CPU tries to access memory below the allocated stack.

On systems with no MMU (eg uClinux), the executable file format usually has a field for the stack size (take a look at the BFLT file format, for instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM