3

I'm trying to understand the CPU's role in tracking a programs stack/heap allocation.

Reading some material, I've come across this:

The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted.

Are the stack and heap pointers stored in program specific registers?

If the stack pointer is pointing to the top of the stack, and (I'm assuming) the heap pointer is pointing to the end of the heap, how would these pointers ever meet without overwriting memory (overflow)?

How does this work in modern systems?

2
  • This is all implementation-dependent. In traditional Unix systems, the heap would grow down from the end of static memory, and the stack would grow up from the bottom of the address space. But there's nothing in the C spec. that requires this, and with modern multi-threaded implementations things aren't as simple. Commented Jun 19, 2016 at 0:37
  • If you want to test this on your system, try doing some tests: Write a program that prints the address of global initialized variable, a global uninitialized variable, some storage returned by malloc, and the address of an automatic (stack) variable. See how they fit into the address space. Commented Jun 19, 2016 at 0:39

1 Answer 1

3

Are the stack and heap pointers stored in program specific registers?

CPUs of stack-based architectures (which represent the overwhelming majority of the CPUs in use today) have a special register for the stack pointer. This is possible because stack, by its very nature, does not get fragmented. Hence, a single pointer is sufficient.

There is no such thing as "heap pointer" because heap is potentially a fragmented data structure. Heap allocators keep a special table of memory fragments available for allocation, and adjust it when the program allocates and releases memory. Memory manager also keeps a pointer to the highest address that has been allocated from the heap.

If the stack pointer is pointing to the top of the stack, and (I'm assuming) the heap pointer is pointing to the end of the heap, how would these pointers ever meet without overwriting memory (overflow)?

Since stack pointer cannot cross without causing an error, many systems limit the size of the stack to a certain number, and make sure that the memory allocator would not let the high point of the heap to cross the upper limit of the stack.

Note: On systems that support concurrency there may be more than one stack active at a time. In this case the stacks are set up next to each other, with the upper limit monitored to detect stack overflows. Here is an article that describes techniques for detecting stack overflows.

Sign up to request clarification or add additional context in comments.

2 Comments

Where does the heap allocator reside? Since it's dealing with program heap, which is a portion of a programs virtual address space, I'm assuming it's something separate from the virtual memory manager?
@wulfgarpro On systems with virtual memory heap allocator usually sits on top of the virtual memory manager, getting big chunks from it, and parcelling them out to individual requests for heap memory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.