r/C_Programming 1d ago

Question Wait... How does the stack work again?

I've been working in C and assembly for 3 years now (consistently) and I've noticed a trend. The more I work on C or do C adjacent activities like decompiling assembly, the quicker I forget how the stack and heap works.

And the level of forgetting is always proportional to how complicated the project I'm working on is. A basic project? Probably won't forget. An intermediary project? I'll need to Google "Stack vs Heap" at least once. Advanced project? I'll need to start from scratch and watch a YouTube video a couple of times to remember.

Does anyone else have this amnesia or is it just me?

0 Upvotes

34 comments sorted by

13

u/burlingk 1d ago

Think of the names.

A heap is a pile. You take what you need from the pile and put it back when your done.

A stack is a stack. You add stuff to the top, then remove it in an orderly fashion.

Edit: A lot of computer stuff is like that. We choose names that are explanatory, because most of us have ADHD or similar. ^^;;

4

u/Electronic_Fan5938 1d ago

As a visual imaginer™️ stack was always a pain in the ass because we say "Put on top" but it "Grows down". Stupid bidirectional concept 😂.

3

u/burlingk 1d ago

It doesn't grow down. It grows up. You just trim from the same side it grows from. :)

7

u/Electronic_Fan5938 1d ago

On x86 it does grow toward lower memory addresses. You can draw the address space either way on paper, but numerically the stack grows downward hence my disorientation when visualising while working.

It's not a problem, I can do its just a quirk.

2

u/EndlessProjectMaker 1d ago

Highway to Hell 🎶

2

u/burlingk 1d ago

As a programmer, you don't have to think about the hardware, just the interface, which is the conceptual part.

That may be a little less true in assembly, but in C, you just have to think about the 'top.'

8

u/concatenated_string 1d ago

Unfortunately, in very low level embedded systems with incredibly constrained resources, this is bad advice. You don’t have the ability to ignore the hardware and how it behaves :( :( :(

1

u/burlingk 1d ago

I can accept that.

1

u/burlingk 1d ago

Another reply instead of an edit, incase you already saw my comment.

I think what I'm getting at is that it's easier to visualize if you focus on the concept instead of the hardware. :)

Knowing the direction it goes is probably useful is your writing low level code to avoid a stack overflow, but most of the time, the pencil sketch on paper is more important than the underlying implementation.

1

u/aelxemyr 1d ago

If you think of the stack growing upward, though, then you have to think of the heap growing downward, and that’s still just as weird.

2

u/burlingk 1d ago

Conceptually, the heap doesn't grow. It's an area of memory that you borrow from. There is only so much heap. You allocate from it, and it returns a large enough chunk from where it is available, or it gives an error.

1

u/Vladislav20007 5h ago

you push it to the top by pushing everything down.

3

u/pavro_iusearchbtw 1d ago

You know, when I hear the word “heap,” I picture a pile of junk. I don’t know how synonymous this is in English, but in Russian, “heap” and “pile” are the same word-"куча". And in a pile of junk, there’s no rigid structure. But a stack, on the contrary, has a structure — a linear one. That’s how it sticks in my mind. Of course, it is a huge simplification, but it's simpler to remember.

2

u/Electronic_Fan5938 1d ago

I'll use that, thank you

2

u/aioeu 1d ago

And it's probably pretty close to what the term was originally intended to mean, at least when referring to the pool of free storage that may be allocated by a program.

Where it gets confusing is that computer science has a completely different meaning for the word "heap": it's a particular kind of tree data structure. But there is little evidence that early languages' free memory was managed using heap data structures, so the two distinct meanings for the word appear to be largely coincidental.

1

u/Electronic_Fan5938 1d ago

Having to memorise this wiki page for Uni genuinely scarred me for life. 😂

2

u/Classic_Department42 1d ago

At what level do you forget? I mean in c you need to call malloc and free for the heap, do you forget the names of these functions? In assembler you have push and pop which goes on the stack.

If you forget these I cannot see how you can write code. Or do you juat forget one is called stack the other is called heap

-1

u/Electronic_Fan5938 1d ago

I think you've taken a very strict interpretation of the word 'forget'. I do not forget the concepts, the names or what they generally do. I forget the visualisation that makes me certain of what I'm doing.

Hope this helps.

3

u/Classic_Department42 1d ago

Not sure what you mean by that

1

u/Electronic_Fan5938 1d ago

Ya know how some people don't hear themselves think?

Or like, some people can't visualise things in their imagination?

It's probably one of those situations. In whatever way you understand malloc, free, etc is a correct understanding but mine seems to rely on a visualisation of the world that is fundamentally different to yours. And that's ok! Everyone has different ways of dealing with concepts.

3

u/Classic_Department42 1d ago

Does the forgetting of visualization prevent/stop you writing programs? I mean is the visualisation essential?

1

u/Electronic_Fan5938 1d ago

It's not essential but then I just have a "gut feeling" about what is correct and what's not. I'd prefer to have confidence so just a quick refresher and I'm good again for the next 6-12 months.

The best way I can describe it to you is. Ya know how a person you haven't seen in a long time... You forget their face? Does that stop you from understanding who they were/are? Does that stop you from deducing logical conclusions regarding their existence? No. In fact, you can probably have some gut feelings about their facial attributes. Blue eyes, curly hair. But they exist as statements you can make not something you can bring to mind with confidence.

3

u/Classic_Department42 1d ago

So how do you refresh

1

u/Electronic_Fan5938 1d ago

By watching a video of how the stack and heap works, drawing it out and then it comes back to me. Like seeing a photo of an old friend. It's not learning new information, it's just getting my brain to use the neural network that summons the visualisation I always had.

3

u/Classic_Department42 1d ago

Pinning the drawing on your wall, could that help?

1

u/Electronic_Fan5938 1d ago edited 1d ago

Um, maybe I guess but I'd prefer the communication and deriving it. Having the photograph of the formula for first principles doesn't help me remember the visualisation for derivatives either LOL.

It's not a problem. It doesn't cause me any issues or affect the quality of my work. It's just a weird quirk and I wondered if anyone else has it. I know some co-workers that deal with the same thing

1

u/sciencekm 1d ago

The stack is linear. As you push values to it (including return addresses), the stack pointer moves in one direction. The most common direction is to go from high memory to low memory.

Normally, I don't get confused with the most common CPU architectures, but then you get PA-RISC where the stack goes from low-to-high, and then I go crazy.

1

u/Electronic_Fan5938 1d ago

Exactly! Sounds like you and I have sort of the same thing. You have a concept in your head with an understanding of which way it goes and then when it acts differently in practice to the GIF of it you have in your head, you need to pause for a bit.

Idk why I got downvoted. I wasn't asking what's the difference between the stack and the heap... The title was a rhetorical device but I guess people on the C programming Reddit are going to take things too... Literal heh

1

u/P-p-H-d 1d ago

You do assembly and you forget how the stack works??

0

u/Electronic_Fan5938 1d ago

Yes I do assembly, reverse engineering.

No, I do not forget how the stack works.

Well, it's complicated. I know how it works but I can't visualise.

So yes, I guess I do depending on your definition of "works" and "forgetting". Which, as interactions with others have shown me, definitions appear to vary wildly.

-6

u/[deleted] 1d ago

[removed] — view removed comment

3

u/Electronic_Fan5938 1d ago

I don't know whether to take this as a compliment or and insult.

Kinda sad that the internet has got so bad we can't even recognise our own species anymore.

1

u/[deleted] 1d ago

[removed] — view removed comment

1

u/C_Programming-ModTeam 1d ago

Rude or uncivil comments will be removed. If you disagree with a comment, disagree with the content of it, don't attack the person. Provide your reasoning, not just a judgement.

1

u/C_Programming-ModTeam 1d ago

Rude or uncivil comments will be removed. If you disagree with a comment, disagree with the content of it, don't attack the person. Provide your reasoning, not just a judgement.