在 C++ 中为零大小分配返回唯一地址背后的基本原理是什么?

What is the rationale behind returning unique addresses for allocations of zero size in C++?

return在 C++ 中为零大小分配设置唯一地址的基本原理是什么?

背景:C11标准关于malloc(7.20.3内存管理函数)的说法:

If the size of the space requested is zero, the behavior is implementation defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

也就是说,正如我所见,malloc 对于零大小的分配总是成功的,因为你唯一能用零大小分配的指针做的就是调用一些其他的内存分配函数,比如 free 与它:

此外,C11(也是 7.20.3)没有指定从 malloc 中 return 的地址必须是唯一的,只是它们必须指向不相交的内存区域:

The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object.

所有零大小的对象都是不相交的 AFAICT,这意味着 malloc 可以 return 相同的指针用于多个零大小的分配(例如 NULL 会很好) ,或每次不同的指针,或某些相同的指针,等等

然后 C++98 出现了两个原始内存分配函数:

void* operator new(std::size_t size);
void* operator new(std::size_t size, std::align_val_t alignment);

请注意,这些函数仅 return 原始内存:它们不会创建或初始化任何类型 AFAICT 的任何对象。

你这样称呼他们:

#include <iostream>
#include <new>
int main() {
    void* ptr = operator new(std::size_t{0});
    std::cout << ptr << std::endl;
    operator delete(ptr, std::size_t{0});
    return 0;
}

C++17 标准的 [new.delete.single] 部分对它们进行了解释,但我认为关键保证在 [basic.stc.dynamic.allocation]:

中给出

Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a non-null pointer value (7.11) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. Furthermore, for the library allocation functions in 21.6.2.1 and 21.6.2.2, p0 shall represent the address of a block of storage disjoint from the storage for any other object accessible to the caller. The effect of indirecting through a pointer returned as a request for zero size is undefined.38

也就是说,它们必须始终 return 不同的成功指针。这与 malloc.

有点不同

我的问题是:此更改背后的基本原理是什么?(即,在 C++ 中 returning 零大小分配的唯一地址背后)

理想情况下,答案只是对探索备选方案并激发其语义的论文(或其他来源)的 link。通常我会选择 The Design and Evolution of C++ 来回答这些 C++98 问题,但第 10 节(内存管理)没有提及任何相关内容。否则,某种权威参考会很好。


免责声明:我asked it on reddit但是我问得不够好,所以我没有得到任何有用的答案。我想请问您,如果您只有一个假设,请随时 post 作为答案,但请注意这只是一个假设。

此外,在 reddit 上,人们一直在谈论零大小的类型,我是否有更改标准的建议等。这个问题是关于原始内存分配函数的语义,当传递的大小等于零。如果诸如零大小类型之类的主题与您的答案相关,请包括它们!但尽量不要因切题而出轨。

此外,在 reddit 上,人们也提出了 "that's for optimization purposes" 之类的论点,但实际上无法提及更具体的内容。我希望答案比 "because optimizations" 更具体。例如,一位 redditor 提到了别名优化,但我想知道哪种别名优化适用于无法取消引用的指针,并且无法让任何人对此发表评论。所以也许如果你要提到优化,一个小例子可以丰富讨论。

问题是 C++ 中的对象(无论它们的大小)必须具有唯一标识。所以不同的共存对象(无论它们的大小)必须有不同的地址,因为两个比较相等的指针被假定指向同一个对象

如果你承认零大小的对象可以有相同的地址,你就无法再区分两个地址是否是同一个对象。


许多关于 "new does not return objects" 问题的评论。

请忘记此上下文中的 OOP 术语:

C++ 规范对 "Object" 这个词的含义有精确的定义。

CPP Reference:Object

特别是:

C++ programs create, destroy, refer to, access, and manipulate objects. An object, in C++, is a region of storage that has

  • size (can be determined with sizeof);
  • alignment requirement (can be determined with alignof);
  • storage duration (automatic, static, dynamic, thread-local);
  • lifetime (bounded by storage duration or temporary);
  • type;
  • value (which may be indeterminate, e.g. for default-initialized non-class types);
  • optionally, a name.

The following entities are not objects: value, reference, function, enumerator, type, non-static class member, bit-field, template, class or function template specialization, namespace, parameter pack, and this.

A variable is an object or a reference that is not a non-static data member, that is introduced by a declaration.

Objects are created by definitions, new-expressions, throw-expressions, when changing the active member of a union, and where temporary objects are required.

原因很简单,代码不应该要求对边界条件进行特殊处理。许多,我想说的是大多数,算法必须将零大小的对象作为边界条件来处理。不太常见的是比较对象指针以查看它们是否是同一对象的算法,但即使对于零大小的对象,这仍然应该有效。

但是,您的问题假定这是一个更改。除了 1980 年代后期的短暂中断外,我所知道的所有 C 和 C++ 实现都一直表现得像这样。

dmr 的原始 C 编译器表现得像这样,但在 1987 年左右,C 标准草案指定零大小对象的 malloc return NULL。这真的很奇怪,甚至最终的 C-89 标准也让它由实现定义,但我从未遇到过做这种可怕事情的实现。

我在 my blog 部分 "Malloc Madness" 中对此进行了更多讨论。