Many ISAs, such as MIPS, has a register called "stack pointer" to indicate the top of the stack. Why isn't a "heap pointer" register? Does the top of the heap get stored in memory?
Why isn't a "heap pointer" register?
There is not a "heap pointer" register, because there is no malloc
CPU instruction.
The reason there is a "stack pointer" register is because there are CPU instructions which directly manipulate the stack, such as call
, push
, and pop
on x86. These instructions generally mandate the existence of an architectural stack pointer register.
Note: AFAIK there are no such instructions on MIPS; you must adjust the stack pointer yourself to push/pop values, and a "call" saves the return address in the Link Register. Still, the concepts of a call stack and local variables are so central to most all languages, that a register is used to track the stack location, because it is much faster than a memory access.
Does the top of the heap get stored in memory?
There are lots of different implementations of malloc
, but they all only take a size
parameter, there is no option but for malloc
to keep state in global variables.