r/cs2a • u/advita_g • Nov 19 '24
Buildin Blocks (Concepts) Stack vs Heap
In the Week 10 and 11 module, one of the topics mentioned that we should study is the difference between the stack and the heap in memory management. At first, I was confused about when and why which would be used, but after some studying (and video-watching), I think I've figured it out. I made a comparison table to help me understand the difference between the two, so I am posting this to help others understand in case they were confused on it too. Please let me know if there is anything else I can add to this/any mistakes, as this is a new concept for me.

4
u/elliot_c126 Nov 19 '24
Thanks for the cheat sheet! I'll be looking over the week 10 and 11 stuff this week so this will be a good table for me to refer to! I have no experience with heaps so should be fun studying this.
3
u/oliver_c144 Nov 20 '24
This is a phenomenal summary, especially since it also discusses how pointers are used to manipulate stack/heap memory. It's really annoying to ping pong myself around the textbook, so I think I'll save this (keep this tab open perpetually)! Some more details about pointers and how they're used would be nice to have, though it's totally optional.
2
u/himansh_t12 Nov 20 '24
Great job creating the table! One thing to remember is that the stack is super fast but has limited space, which makes it great for short-term tasks like storing variables inside functions. The heap, on the other hand, has way more room and is used for long-term storage, but it’s slower because you have to manage it yourself (like cleaning up memory when you’re done). You kinda pointed it out but I just wanted to be more direct with the point! I hope this info is useful to you!
-Himansh
4
u/nancy_l7 Nov 20 '24
This is such a great resource, especially for anyone else starting platypus now/soon! Before, I basically only knew the general differences between a stack and a heap, but after reading through your comparison table, I felt like I gained a much more sophisticated understanding of the two concepts. For example, I learned how pointers function in stack versus heap memory, as well as how they are used to get addresses in different ways.
One thing I wanted to add based on what I've read from your info and online, as a sort-of summary, is that a heap seems to be much more flexible than a stack. For example, a stack's elements can only be accessed/manipulated from the "top" (last value); while a heap's memory can be allocated or deallocated dynamically during runtime, so one can create more complex
thingsdata structures(?) like (referring to week 10/11 topics) linked lists.-Nancy