Humberd
Humberd

Reputation: 2951

Understanding a destructor

I have the following simple class Foo:

class Foo {

};

And I try to run the following Unit Test code:

Foo* foo = new Foo;

Assert::IsNotNull(foo);

delete foo;

Assert::IsNull(foo); //why is it not null?? I deleted it.

When I delete foo the memory for this object was freed, so that the foo should point to the nullptr.

Why the foo is not null after deleting it?

Upvotes: 0

Views: 49

Answers (1)

Paul Stelian
Paul Stelian

Reputation: 1379

Deleting will not change the value of the pointer, but only release the memory it points at. After deleting, the respective pointer is invalid. (deleting will also call the destructor).

Deleting a valid pointer must always succeed, otherwise it's undefined behavior.

Upvotes: 4

Related Questions