-5


I observed some unexpected behavior with the delete semantics. For example, using the following C++ code:

class Base{
    public:
         Base(int tmp) : x(tmp) {}
         ~Base() { std::cout << "Inside Base::~Base()" << std::endl; }
         void foo() { std::cout << "Inside Base::foo()" << std::endl; }
         int x;
};
...
int main(int argc, char** argv)
{
    Base* b = new Base(10);
    delete b;
    b->foo(); 
    std::cout << "b->x: " << b->x << std::endl;
}

I received the following output from Visual Studio 2008:

Inside Base::~Base()
Inside Base::foo()
b->x: 2359492

Why after the call to delete b, I am still able to call the Base::foo() method?

Thanks, Chris

user881285
  • 55
  • 1

3 Answers3

2

As others already have pointed out in the comments this is Undefined Behavior. This means that the implementation can do whatever it wants, including returning the value you expect.

It could also decide to format your hard drive or make the famous demons fly out of your nose.

For a few anecdotes as to what may happen if you invoke UB, see this question

Community
  • 1
  • 1
Hulk
  • 6,399
  • 1
  • 30
  • 52
2

Why after the call to delete b, I am still able to call the Base::foo() method?

Because, as with all undefined behaviour, there is no requirement for the program to fail gracefully.

In this case, there is no guarantee that deleted memory becomes inaccessible; and even if it does, calling a non-virtual member function is unlikely to access that memory anyway. So, unless the program specifically checks whether the object is valid before calling the member function (which would be very expensive, so no implementation would do that by default), there's every chance that the function would be called as expected and the bug hidden.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

After you delete something it gets recycled by the heap. This means that you have no guarantee that your old information will be there if you hadn't accessed it immediately after deleting it. The reason why people say not to access it is because you have no control over what it could be. This type of garbled output is "expected" undefined behavior after the deletion of a variable.

As @LuchianGrigore said, "Don't do it"!!!!!

CoderDake
  • 1,497
  • 2
  • 15
  • 30
  • 1
    In C++ nothing gets recycled by the OS after deleting. In fact, deleting only marks the memory as free. As long as you do not reclaim memory, it values stay the same. – Martin Schlott Sep 09 '13 at 15:21
  • 1
    @MartinSchlott: That depends on the implementation. It's common for large allocations to come directly from the OS, while small ones come from a heap maintained within the process. (Of course, if it's been returned to a modern OS, then you'll get a protection fault rather than another process's data if you try to access it.) – Mike Seymour Sep 09 '13 at 15:28
  • I changed my answer to a little bit less specific explanation. It will definitely be placed back in the heap after being deleted. Thanks for straightening me out :P – CoderDake Sep 09 '13 at 15:38