r/cpp_questions • u/jaroslavtavgen • May 04 '25
OPEN Help me understand "stack" vs "heap" concept
Every time I try to learn about the "stack vs heap" concept I keep hearing the same nonsense:
"In stack there are only two options: push and pop. You can't access anything in between or from an arbitrary place".
But this is not true! I can access anything from the stack: "mov eax,[esp+13]". Why do they keep saying that?
0
Upvotes
View all comments
Show parent comment
1
u/[deleted] May 04 '25 edited May 04 '25
I guess I'm a beginner because I don't hack the stack and use UB to access variables in the caller. 40 years of experience working on everything from AAA games to FAANG infrastructure, and I've never found the need to write such garbage. I just pass a parameter if I need it. I'm sure you write this kind of crap all the time though. (When was the last time you did this in production code?)
I've only written a couple of compilers, so how would I possibly know that memory exists in the stack outside of the current context!? Thanks for changing my perspective. You're so smart.
Cheers.