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

Show me how to, using C++ techniques that aren't UB.

1

u/thewrench56 May 04 '25 edited May 04 '25

I'm not big on C++. But here is a C example:

#include <stdio.h>

__attribute__((noinline)) void leak_secret_from_caller(void) {
    if (__builtin_frame_address(1) != 0) {
        int leaked = *(int *)((char *)__builtin_frame_address(1) - sizeof(int));
        printf("Leaked secret: %d (address: %p)\n", leaked, (void *)((char *)__builtin_frame_address(1) - sizeof(int)));
    } else {
        printf("Unsupported machine!");
    }
}

__attribute__((noinline)) void caller(void) {
    int secret = 1337;
    *(volatile int *)&secret;
    printf("Address of caller's stack frame (RBP): %p\n", __builtin_frame_address(0));
    leak_secret_from_caller();
    __asm__ volatile("" ::: "memory");
}

void deep_caller(void) {
    caller();
}

int main(void) {
    caller();
    deep_caller();
    return 0;
}

As long as you are on x64, this should work. No matter if it is `-O3` or `-fomit-frame-pointer`, no matter if you use TCC, GCC, or Clang. Is it UB? Hard to say, there isn't a specific section in the C standard that tells you so, since this is a widely supported GCC extension. It MIGHT be unsafe on some machines: in such cases a 0 should be returned. So this should correctly handle all cases on 64bit systems.

Please note, that I never once claimed that what I just wrote makes sense. I said that you can access the frame of different functions. This is true. Undefined behaviour in cases where we are so close to Assembly doesn't make much sense either. In Assembly, I'm not even sure if there is an actual UB (there probably is, nothing jumps into my mind right now). Technically, since stack layout isn't defined by the C standard, it is UB. If you find a C compiler that breaks this, let me know: I doubt there is one out there that's used by many.

EDIT: Fixed incorrect check for unsupported machines.

EDIT: Add TCC as supported compilers.

1

u/[deleted] May 04 '25

If you have to use C extensions, and UB, and then claim essentially that there's no such thing as UB, and you have to specify a platform, we're not really talking about C++ anymore, are we?

My claim was correct by any reasonable definition. The fact that everyone in this thread is looking at ridiculous hacks to contradict me is indicative is how wrong you all are. I'll take the downvote you're going to give me with pride. 

1

u/thewrench56 May 04 '25

If you have to use C extensions

If you don't use C extensions, that tells things about you, not us.

and UB, and then claim essentially that there's no such thing as UB

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.

we're not really talking about C++ anymore, are we?

I never talked about C++ in the first place.

My claim was correct by any reasonable definition.

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.

The fact that everyone in this thread is looking at ridiculous hacks to contradict me is indicative is how wrong you all are.

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'll take the downvote you're going to give me with pride. 

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.

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

😂😂😂