Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of value does a pointer hold after using it to explicitly call the pointed object's destructor?

From https://timsong-cpp.github.io/cppwp/basic.compound#3 :

Every value of pointer type is one of the following:

  • a pointer to an object or function (the pointer is said to point to the object or function), or
  • a pointer past the end of an object ([expr.add]), or
  • the null pointer value for that type, or
  • an invalid pointer value.

After using a pointer to explicit call an object's destructor, which of these four kinds of value does the pointer have? Example :

#include <vector>

struct foo {
    std::vector<int> m;
};

int main()
{
    auto f = new foo;
    f->~foo();
    // What is the value of `f` here?
}

I don't believe it can be a pointer to an object or function. There is no longer an object to point to and it is not a function pointer.

I don't believe it can be a pointer past the end of an object. There wasn't any sort of pointer arithmetic and no array is involved.

I don't believe it can be a null pointer value since the pointer is not nullptr. It still points to the storage the object had, you could use it to perform placement new.

I don't believe it can be an invalid pointer value. Invalid pointer values are associated with the end of storage duration, not object lifetime. "A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration". The storage is still valid.

It seems to me like there is no pointer value the pointer could have. Where did I go wrong?

like image 572
François Andrieux Avatar asked Sep 07 '20 00:09

François Andrieux


2 Answers

It's the pointer to the object, but the object is just not within its lifetime.

In [basic.compound], footnote 42):

For an object that is not within its lifetime, this is the first byte in memory that it will occupy or used to occupy.

like image 194
yuri kilochek Avatar answered Sep 24 '22 14:09

yuri kilochek


It is a pointer that pointed to the object that no longer exists. Of the possible values that the standard lists, only pointer to an object can apply, but only if we consider objects outside of their lifetime to be included in that definition. Invalid could apply if that could include pointers to storage with no objects.

If neither of these can apply, then the list of all values would be defective.


If you were to create a new object into the storage, then this would apply:

[basic.life]

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if the original object is transparently replaceable (see below) by the new object.

like image 45
eerorika Avatar answered Sep 23 '22 14:09

eerorika