gpt4 book ai didi

c++ - 指针/整数算术(未)定义的行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:59 25 4
gpt4 key购买 nike

我有以下函数模板:

template <class MostDerived, class HeldAs>
HeldAs* duplicate(MostDerived *original, HeldAs *held)
{
// error checking omitted for brevity
MostDerived *copy = new MostDerived(*original);
std::uintptr_t distance = reinterpret_cast<std::uintptr_t>(held) - reinterpret_cast<std::uintptr_t>(original);
HeldAs *copyHeld = reinterpret_cast<HeldAs*>(reinterpret_cast<std::uintptr_t>(copy) + distance);
return copyHeld;
}

目的是复制一个特定类型的对象,并返回由与输入相同的子对象“持有”的对象。请注意,原则上,HeldAs 可以是 MostDerived 的模棱两可或不可访问的基类,因此这里没有任何转换可以提供帮助。

这是我的代码,但它可以用于我无法控制的类型(即我无法修改 MostDerivedHeldAs)。该函数具有以下先决条件:

  • *original 是动态类型MostDerived
  • HeldAsMostDerivedMostDerived 的直接或间接基类(忽略 cv 限定)
  • *held 指代 *original 或其基类子对象之一。

让我们假设先决条件已满足。在这种情况下,duplicate 是否定义了行为?

C++11 [expr.reinterpret.cast] 说(大胆强调我的):

4 A pointer can be explicitly converted to any integral type large enough to hold it. The mapping function is implementation-defined. [ Note: It is intended to be unsurprising to those who know the addressing structure of the underlying machine. —end note ] ...

5 A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined. [ Note: Except as described in 3.7.4.3, the result of such a conversion will not be a safely-derived pointer value. —end note ]

好吧,假设我的编译器是 GCC(或 Clang,因为它使用 GCC 对实现定义行为的定义)。引用 GCC docs chapter 5关于 C++ 实现定义的行为:

... Some choices are documented in the corresponding document for the C language. See C Implementation. ...

转到 chapter 4.7 (C 实现、数组和指针):

The result of converting a pointer to an integer or vice versa (C90 6.3.4, C99 and C11 6.3.2.3).

A cast from pointer to integer discards most-significant bits if the pointer representation is larger than the integer type, sign-extends if the pointer representation is smaller than the integer type, otherwise the bits are unchanged.

A cast from integer to pointer discards most-significant bits if the pointer representation is smaller than the integer type, extends according to the signedness of the integer type if the pointer representation is larger than the integer type, otherwise the bits are unchanged.

到目前为止,还不错。这似乎是因为我使用的 std::uintptr_t 保证对任何指针都足够大,而且因为我处理的是相同的类型,copyHeld应该指向 *copy 的相同 HeldAs 子对象,因为 held*original 中指向。

不幸的是,GCC 文档中还有一段:

When casting from pointer to integer and back again, the resulting pointer must reference the same object as the original pointer, otherwise the behavior is undefined. That is, one may not use integer arithmetic to avoid the undefined behavior of pointer arithmetic as proscribed in C99 and C11 6.5.6/8.

砰的一声。所以现在看来​​,虽然copyHeld的值是按照前两段的规则计算出来的,但第三段还是把它送进了Undefined-Behaviour land。

我基本上有三个问题:

  1. 我的阅读是否正确,duplicate 的行为是否未定义?

  2. 这是哪种未定义行为?是“形式上未定义,但无论如何都会做你想做的”,还是“预计会发生随机崩溃和/或自焚”?

  3. 如果它真的是未定义的,有没有办法以定义明确(可能依赖于编译器)的方式来做这样的事情?

虽然就编译器而言,我的问题仅限于 GCC(和 Clang)行为,但我欢迎考虑各种硬件平台的答案,从普通桌面到奇异的平台。

最佳答案

通常的模式是在基类中放置一个clone()
然后每个派生类都可以实现自己的克隆版本。

class Base
{
public:
virtual Base* clone() = 0;
};

class D: public Base
{
virtual Base* clone(){ return new D(*this);}
};

关于c++ - 指针/整数算术(未)定义的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25265824/

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