Skip navigation
Difference between Heap and Stack
0:00 / 2:14

Difference between Heap and Stack

859 likesDownloadOriginal post

174,492 views

Difference between Heap and Stack

14 Comments

Comments from the original post on X (Twitter)

  • Sebastian Aaltonen's profile picture

    @SebAaltonen

    Allocating/deallocating from stack is fast because those are just add/sub to a pointer. Stack is also linear and the most recent data is in top, so it's very likely in L1$, which is just 5 cycles away from registers. Heap is scattered around the whole address space. Very likely cache miss and very expensive alloc/dealloc, since there's a complex data structure for the allocator and it must be protected from races (mutex) in multithreaded systems. Stack is per-thread, so there's never races there. No protection needed. Just a trivial non-atomic add/sub to manipulate it. Push/pop are not that important instructions nowadays. You simply fuse two fast instructions together shaving one cycle (not even that nowadays due to super wide execution). But spilling to stack is common, so might as well make that common operation slightly faster.

    97 likes

  • Tom Miller's profile picture

    @TommyYesItsMe

    If you look at the GCC and TCC source code, they use heap very sparingly and stack only for local variables. But they use extensively static memory. The kind of C memory nobody talks about. Maybe C and C++ compiler devs know something about these languages we missed all along.

    8 likes

  • TheStandupPod's profile picture

    @thestanduppod

    Full Ep:

    4 likes

  • Daniel Vega's profile picture

    @danielmvega

    How come that the return address is not stored in RAM?

    3 likes

  • Matthew Barlowe's profile picture

    @Matt_barlowe

    A heap is where is just put things a stack is where i put things neatly

    2 likes

  • deadalnix's profile picture

    @deadalnix

    Another major difference is that the optimiser know the stack doesn't alias, so it can optimise much more. At every single level, from hardware to higher level languages, and everything in between, the staok is treated differently.

    2 likes

  • Yawar Amin's profile picture

    @yawaramin

    OCaml is this simple language that does that very direct translation btw let n = 10 (* int, on stack *) let s = "hello" (* string, on heap *)

    2 likes

  • Oscar Hedeby Iversen's profile picture

    @HedebyIversen

    This is dumb, how would this ever make my JavaScript go faster?

    1 like

  • Otis Crune 🇺🇲🌎🚀's profile picture

    @RealOtisCrune

    Odin would make a great learning language—very explicit about important lowish-level concepts while remaining super legible.

    1 like

  • Alexandru Bogdan's profile picture

    @AIexandruBogdan

    Some of my favourite people on this podcast. Top Top!

    1 like

  • 0x44's profile picture

    @0x44_

    Never heard of a stack stored on a special core. Maybe a modern convention tied to branch prediction, but traditionally there's no separate stack, and at least with C, it's up to the compiler designer to decide where things go based on the ISA, i.e. registers vs stack.

  • kimiyoribaka's profile picture

    @kimiyoribaka

    I think this is particularly interesting to look at for Wasm, where you have the Wasm stack that code can't access ever and then to compensate you have the stack that compilers like to make so function calls are easier to deal with, all of which are on JS heap.

  • facts matter, lies shatter 🇳🇱 🇺🇸's profile picture

    @fctsMttrLsShttr

    If you do not use recursion and limit the use of function pointers you can calculate the stack usage at compile time. But if you use dynamic allocation it's much harder to predict total heap usage and the chance of heap fragmentation. I avoid using the heap in my firmware.

  • Alexander Torstling's profile picture

    @atorstling

    There's also a fundamental difference in allocation strategy. Things allocated on the stack can't grow once more stack frames are added on top. This means that you need "side channels" for growing.