2 ms·
Sorry if OT, but the nand2tetris course really brought the benefits of stack vs heap allocation into stark relief. As part of the course, you have to implement
by SilasX 8y ago
Sorry if OT, but the nand2tetris course really brought the benefits of stack vs heap allocation into stark relief. As part of the course, you have to implement a compiler and OS, including malloc/free [1].
You can see how local variables just push something onto the stack, which is a small number of CPU instructions, while malloc is a big function, each step of which translates into many instructions, and which also has to deal with iterating through a data structure to find a big enough block.
Part 8: Implementing the virtual machine, where you do stack allocation/calls: https://www.nand2tetris.org/project08 https://www.nand2tetris.org/project08
Part 12: Implementing the OS, where you add the malloc/free utilities (Memory.jack): https://www.nand2tetris.org/project12 https://www.nand2tetris.org/project12
[1] It calls them alloc and deAlloc in the course.