0

Here is the code:

#include <iostream>

using namespace std;

class A {
public:
    void print() {
        cout << "Object is still alive" << endl;
    }
};

int main() {
    try {
        A a1 = A();
        A* a = &a1;
        throw a;
    }
    catch (A* a) {
        a->print();
    }
}

Why is object a1 still alive in the catch block (you can check it for yourself, print method works) even though I have thrown exception of type pointer to an object of class A? I thought that all local objects in the try block get destroyed as soon as we leave it?

  • 3
    This program exhibits undefined behavior. "Seems to work" is one possible manifestation of undefined behavior. In fact, in your example even `((A*)nullptr)->print();` would seem to work, as `print` doesn't actually access the object it's called on. Put another way: what did you expect to happen instead, and why did you expect that particular outcome? – Igor Tandetnik Jan 23 '22 at 15:41
  • 2
    https://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer – Hans Passant Jan 23 '22 at 15:41
  • 1
    It is a bit weird that the [sanitizers](https://godbolt.org/z/Kxf5deGPa) don't appear to catch this particular case. Still a good idea to compile with them on. – Nathan Pierson Jan 23 '22 at 15:46
  • 1
    Exactly, what did you expect to happen when you called `a->print()`? A message box popping up saying "invalid instruction"? Or the program ignoring that line? Whatever answer you give, someone else can give a different answer -- that's what "undefined behavior" is all about. – PaulMcKenzie Jan 23 '22 at 15:48
  • Well I expected my program to crash because I thought that object a1 gets destroyed as soon as we leave try block. So I don't understand why it is possible to call print method for that object in the catch block as if it is still alive. Is that object still alive in catch block or not? – Pavle Šarenac Jan 23 '22 at 15:51
  • 3
    Does this answer your question? [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/a/6445794/5980430) – apple apple Jan 23 '22 at 15:52
  • 2
    @PavleŠarenac There is no standard, guaranteed way to make a program crash. – PaulMcKenzie Jan 23 '22 at 15:52
  • 2
    *Is that object still alive in catch block or not?* -- Look at it this way -- if the authorities take away your driver's license, does that mean you can't physically get into a car and drive it? You can still drive a car. Now when you drive the car, are you guaranteed to be stopped by a policeman? No, there is no guarantee that you will be stopped by a policeman. But regardless, it is illegal for you to drive a car, just like it's illegal for you to access a dangling pointer. Accessing the dangling pointer does not guarantee a "crash" or failure. But it might do so. – PaulMcKenzie Jan 23 '22 at 15:57

1 Answers1

3

Why is object a1 still alive in the catch block

It's no longer alive.

you can check it for yourself, print method works

That proves nothing about the lifetime of the object.

I thought that all local objects in the try block get destroyed as soon as we leave it?

You have been thinking correctly.


The behaviour of the program is undefined.

I expected my program to crash

This is where you made the biggest mistake. The program is not guaranteed to crash when the behaviour is undefined. You cannot rely on such assumption.

eerorika
  • 232,697
  • 12
  • 197
  • 326