c - Why is memory allocation on heap MUCH slower than on stack? -


i have been told many times. don't know why...what cost involved when allocating memory heap? hardware related? related cpu cycles? many guesses no exact answers...could give me elaboration?

just "unwind" said, heap data structure more complicated stack. , in opinion, memory space allocated thread stack when starts run, while heap shared threads within process. paradigm require mechanism manage each thread's usage of shared heap, such garbage collection. right on this?

because heap far more complicated data structure stack.

for many architectures, allocating memory on stack matter of changing stack pointer, i.e. it's 1 instruction. allocating memory on heap involves looking big enough block, splitting it, , managing "book-keeping" allows things free() in different order.

memory allocated on stack guaranteed deallocated when scope (typically function) exits, , it's not possible deallocate of it.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -