Why heap needs additional memory for management while stack doesn't?

I read an article that says memory allocated on heap needs additional memory for management.

Say,if we try to allocate 200 bytes,there'll be additional 8 bytes allocated for memory management.

But stack doesn't require this additional space.

I know stack addresses from high=>low,but heap low=>high,but why is the additional space needed on heap?


Stack variables are allocated as part of the stack frame, which goes away on ret . This isn't actually 100% free; there's a register which keeps track of where the stack frame starts ( %ebp on x86) and it needs to be saved on function entry and restored before exit; but it's per function instead of per allocation, and tracking the frame base pointer is useful for more than just this (in particular, it's used to unwind the stack during exception handling), so it's something of a sunk cost.


The stack is simpler to manage (you always allocate/free from the top) and the stack is already managed by the stack pointer. That is why is needs less "administration space" than the heap where you are basically free to do anything.

链接地址: http://www.djcxy.com/p/78760.html

上一篇: Java堆栈和堆内存管理

下一篇: 为什么堆栈需要额外的内存用于管理,而堆栈不需要?