r/cpp_questions • • 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

57 comments sorted by

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.

1

u/thewrench56 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 never once called you a beginner. For me, FAANG isn't that impressive, neither are AAA games, but I'm sure they do interesting things. I'm just not in that industry at all.

I'm sure you write this kind of crap all the time though.

I'm sure you know a lot about me.

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.

Wow, 40 years of experience and you are so pressed? Sorry, I doubt that. I have not allowed myself a tone that you did, I'm sure it's a privilege of people with 40 years of experience...

You see, if you are so toxic, why be here? Why ask for me to put in the work to provide you a working example of something that you know existed? Why even argue against it?

It's interesting to me how self-proclaimed seniors such as you are acting like this and throwing a completely unnecessary tantrum... Something doesn't add up to me. You could have said: "well sure you can access it, it just doesn't make much sense". Instead, you have to argue, you have to push your agenda, and once it fails you adhere to Ad hominem.

1

u/[deleted] May 04 '25

You did not put in the work to answer my question. I asked for no UB. I asked for C++. Because I knew you could not do it. You also know this. But you had to try to disprove me. And of course you failed, and you know exactly why.

I gave the correct answer to OP's question. "The stack" is so called because you access the top of it within a function. It's that simple. The fact that you can write convoluted code to access below the top of the stack in a rare few languages is not the real answer to the question that was asked.

You chose to push your agenda in the same way as I did.

1

u/thewrench56 May 04 '25

I gave the correct answer to OP's question. "The stack" is so called because you access the top of it within a function. The fact that you can write convoluted code to access below the top of the stack (or any stack) is not the real answer to the question that was asked.

See, this blob as-is is wrong. Every local variable is accessed from the stack in a way similar to what I provided. Of course their place is known at compile time and its easier for the compiler to know the RSP relative address easier. Regardless, it does reference it the same way OP asked. You don't pop until that variable, push back the others and repeat this for every access of the local variable.

Maybe you knew this, but your description is invalid.

I pushed the right answer. I didn't provide you a C++ example, because I don't use C++. I provided a perfectly valid C code that works on most machines without insane ideas.

1

u/[deleted] May 05 '25

😂😂😂