r/cs2a • • 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.

9 Upvotes

6 comments sorted by

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 things data structures(?) like (referring to week 10/11 topics) linked lists.

-Nancy

1

u/advita_g Nov 21 '24

Hi Nancy,

I was really confused about this concept at first (if stacks in memory does last-in-first-out), but here's what I found.

The stack data structure is like an array with some distinctions (uses LIFO, implementation differences, etc). What I found is that in this data structure, as you said, a stack's elements can only be accessed/manipulated from the "top" (last value).

However, stack memory as a memory management region is not a data structure, it is where (some) data is stored in a program. It also follows LIFO, but in a different way. You would still be able to access any of this stack's elements.

A stack frame is a part of the memory in the stack. Suppose you have a function1 that calls function2 that calls function3. In the stack, you would have the main stack frame, then function1 stack frame when function1 is called. Then, when function2 is called, function2 stack frame would get added to the stack and same when function3 is called. Then, function3 ends first so it is deallocated FIRST. Then function2, then function1. The last-in function is the first-out.

This was my understanding, and it's pretty challenging for me to understand so please let me know if this is inaccurate.

2

u/nancy_l7 Nov 21 '24

Ah I see, thank you for explaining the last-in-first-out concept and stack frames in a step by step manner. I feel like I'm better able to understand the memory in a stack now.

-Nancy

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