We know that each thread has its own stack. Where are these stacks allocated? I read some documents, it seems the stacks are allocated from heap when a thread is created. Does this make sense? Thanks in advance!
-
Details on how Linux thread stacks are allocated: [Where are the stacks for the other threads located in a process virtual address space?](https://stackoverflow.com/q/44858528) and the fact that growing them dynamically doesn't work. – Peter Cordes Aug 12 '21 at 12:31
2 Answers
C doesn't specify where the memory comes from. It's dependent on the OS and the C runtime library. Either thread stack memory is allocated by the operating system as part of the system call that creates a thread, or the process creating the thread has to provide memory from the application heap to be used as thread stack.
Looking at the documentation for the Linux clone syscall, it appears that Linux does the latter; thread stack memory comes from the application heap.
Why do you ask?

- 18,516
- 4
- 43
- 50
-
1The reason I am asking this question is that I am a little bit confused about stack and heap. Usually people say that heap grows upward and stack grows downward and possibly they can step on each other. But in multi-threading case, if stack comes from heap, can stack or heap still grow into the other one? Thanks in advance. – user3645364 May 16 '14 at 17:58
-
That's also system-dependent, but usually there's some sort of "guard page" set up that raises an exception if a thread stack overflows. On 64-bit systems it's of course theoretically possible to allocate a large amount of (contiguous) virtual address space for each thread stack in advance, backed by physical memory as needed, but I don't know if that's commonly done. – Russell Borogove May 16 '14 at 18:13
-
Thanks for your answer. I need ask the question a little bit further to make me understand. For a mutli-process system, how many heaps does the system have? Each process has its own heap or all the processes share one global heap? How does brk() or sbrk() work for a multi-process system? Thank you very much in advance. – user3645364 May 16 '14 at 18:22
-
Each process has its own heap. I'm a little vague on how brk/sbrk work, but I think the process starts with a lot of virtual address space allocated, and a modest amount of physical memory, and brk/sbrk commit more physical memory to back the virtual. Contiguous virtual space can be backed with discontiguous physical memory. – Russell Borogove May 16 '14 at 18:31
-
1Thanks very much for your answer. It makes sense to me. So even if all the processes have different virtual memory space, but when memory allocation routines (malloc() or new()) are called, ultimately the memory comes from the common physical memory space ( committed to process by brk/sbrk). That's why I heard whether new() or malloc() succeeds depends on availability of whole system memory. – user3645364 May 16 '14 at 18:47
-
1Yeah, but the VM system can also swap to disk when committed virtual memory space exceeds physical memory available. – Russell Borogove May 16 '14 at 19:13
Some systems have built in support for threads. Others do not. Thread libraries for non-supporting systems will allocate stack space from the heap and implement thread switching using timers.
A system that has built in support for threads will usually create the stack by making new pages into the addresses space. Ideally, it will put a guard page at each end of the stack do detect overruns or under runs.

- 20,574
- 3
- 26
- 62