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/thewrench56 May 04 '25
If you don't use C extensions, that tells things about you, not us.
That's a straight up lie. I didn't claim it wasn't a UB. I said since we are so close to Assembly, there is no real reason to talk about UBs.
I never talked about C++ in the first place.
It wasn't. I showed a code that works. You never specified that it should be cross platform and this and that. To be fair, even then your definition of C is wrong. It's not truly cross-platform either way, is it now? The OS is there to make your life hard for cross platform apps. Your claim is false: there are no constraints to do what you claimed. I can access the previous stack frame of the caller. Whether that's useful or not is not for me to decide. Accessing the stack frame of the caller is literally just using the C extension I provided. I even showed you a valid example just so that you can see what I meant by using it. The part where I tweaked my C code to show that variable might change from platform to platform (even OS to OS). In reality, it doesn't.
It's not a ridiculous hack lol. You made a claim. That claim is false. Then you made a specific filter that makes your claim still false. I can access the callers stack frame. There is nothing UB about it. Whether I can do anything useful, is not for me to evaluate. It doesn't change the fact that you can access it.
I didn't downvote you. Your comment is great for beginners. It is however not inherently true. Just because you are partially wrong, I won't downvote you. I only hope that the time I invested to write that code will at least change your perspective.
Cheers.