gpt4 book ai didi

c++ - 使用指针显式调用指向对象的析构函数后,指针持有什么样的值?

转载 作者:行者123 更新时间:2023-12-01 14:30:40 26 4
gpt4 key购买 nike

来自 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.

使用指针显式调用对象的析构函数后,指针具有这四种值中的哪一种?例子 :
#include <vector>

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

int main()
{
auto f = new foo;
f->~foo();
// What is the value of `f` here?
}
我不相信它可以是指向对象或函数的指针。不再有可指向的对象
它不是函数指针。
我不相信它可以是超出对象末尾的指针。没有任何类型的指针算术,也没有涉及数组。
我不相信它可以是空指针值,因为指针不是 nullptr .它仍然指向对象所拥有的存储空间,您可以使用它来执行放置 new .
我不相信它可以是无效的指针值。无效的指针值与存储持续时间的结束相关联,而不是与对象生命周期相关联。 "A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration" .存储仍然有效。
在我看来,指针可能没有指针值。我哪里做错了?

最佳答案

它是指向对象的指针,但对象不在其生命周期内。
[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.

关于c++ - 使用指针显式调用指向对象的析构函数后,指针持有什么样的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63770160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com