Heap access vs stack access | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Heap access vs stack access

Hi I have idea that heap memory allocation is slower as it goes to kernal level call and hence making it slower than stack allocation. Once allocation is done, is it still slow to access heap allocated memory compared to stack allocated variable ? If yes , why ?

16th Oct 2022, 12:56 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 1
After allocation, access to memory would operate at the same speed, regardless of heap or stack. https://stackoverflow.com/questions/24057331/is-accessing-data-in-the-heap-faster-than-from-the-stack "The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free." Note: I arrived at this answer by typing into Google: "heap stack speed" and taking the first suggested hit. If you follow the link to stackoverflow, there are much longer and more technical explanations too.
18th Oct 2022, 4:35 AM
Tibor Santa
Tibor Santa - avatar