0

I don't understand how this code works:

class AAA { public: short a, b; }; AAA &getRef() { AAA aaa = {2, 6}; return aaa; } // 'aaa' is destroyed here right? int main() { AAA &ref = getRef(); cout << ref.a << ", " << ref.b << endl; cin.ignore(); return 0; } 

Shouldn't there be an error trying to access ref.a and ref.b? When I use pointers, I don't get an error either. I mean,this prints "2, 6" every single time.

EDIT: Is it because the memory is still set to those numbers?

19
  • 12
    It's undefined behaviour, not just an error. Commented Sep 16, 2013 at 22:25
  • 7
    @Susan So what? It's still undef behaviour. It might blow up next time. Or worse, once you write code that is deployed on your company's customers' systems. Commented Sep 16, 2013 at 22:27
  • 4
    @SusanYanders: Getting the same results is one possible behavior when you have undefined behavior. Commented Sep 16, 2013 at 22:27
  • 3
    Please don't remove the content of your post because of the answers. Commented Sep 16, 2013 at 22:34
  • 1
    @RickyBeam I think you have some misconceptions about how references work. Commented Sep 16, 2013 at 22:35

4 Answers 4

7

It "works" because the memory for aaa hasn't been overwritten when the function returns. If you modify the AAA class to have a destructor that modifies a and b, or if you use some code that writes to the stack, it will almost certainly overwrite the values.

Returning a reference to a local variable is defined by the C++ standard as a "undefined behaviour". The standard often does this in cases where it may, for example, be difficult to determine that the value is indeed stack-based.

For example, consider:

class BBB { AAA& x; public: BBB(AAA& a) : x(a) {} AAA& getX() { return x; } }; AAA& getReg() { AAA aaa = { 2, 6} BBB bbb(aaa); return bbb.getX(); } 

Most modern compilers will issue a warning for the scenario you have, and some may also give a warning for the code I just wrote. But it's almost certainly possible to come up with some more convoluted case for where it's NOT possible to diagnose this "error".

Sign up to request clarification or add additional context in comments.

Comments

6

Unfortunately this is illegal code that invokes undefined behaviour, but it is not flagged as error by the compiler (although it might if more compiler diagnostics are enabled).

It happens to work by accident, presumably because the memory location where the object is created is not re-used for anything else, and is not cleared or overwritten, so you are just "lucky" that the values, once put there, remain.

2 Comments

you didnt even answer my question. i wanted to know why the memory was still allocated when it went out of scope.
@SusanYanders He did answer, in a way. The memory is not "still allocated". It was momentarily until the return aaa, not anymore. The memory is still impregnated with the values it had when it was allocated, though. Reason being that "releasing" memory simply means that the area of memory (number of bytes) that was temporarily reserved to some usage is not reserved anymore. And until another part of your program (or the system or...) re-reserves it and writes in it, the values you put there initially remain as is.
2

It seems you want "proof" that it can fail. Maybe try:

int main() { AAA &ref = getRef(); cout << "Hello, world!" << endl; cout << ref.a << ", " << ref.b << endl; } 

also, you should enable and pay attention to your compiler's warnings.

Comments

2

Replace the shorts with a user-defined type with a complicated destructor (like string), and you will probably see this crash. Because the memory has been reclaimed, but the old value is still sitting there, you can (sometimes) see the value of built-in types (like int, short, etc.) even after destruction.

[Sample Code]

2 Comments

You can sometimes see the value of non-built-in types too. There's no reason it has to crash just because of a user-defined destructor.
In my experience, you're a lot more likely to crash dereferencing deleted pointers than looking at reclaimed stack memory. Since it's undefined, your experiences almost certainly differ. :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.