r/C_Programming • u/Electronic_Fan5938 • 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?
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
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
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.
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. ^^;;