3

The draft C++11 standard says

Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses. (1.8(6))

However, earlier it says an object can be created by a new expression, and it's conceivable that a new expression could cause a user-defined operator new() to be called that could return the address of some char[] block it's reserved for this purpose (the first allocation could return the first address of the block), which would mean there are two objects that do not have distinct addresses (the one created by new and the char[] block). Does 1.8(6) mean that it's illegal for a user-defined new to work this way? Or is it just a hole in the language definition?

4

1 回答 1

2

显然,如果您使用 achar[]来分配地址作为“自定义” operator new,那么该char[]对象将与您创建的对象重叠。

只要您不同时将char[]对象用于其他用途,这不是真正的问题,这不是本节要讨论的内容。

这并不是真正的目的。如果您确实创建了自己的operator new,则不应允许它为其创建空间的对象返回相同的地址(或重叠地址)。那将打破上述规则。

于 2013-09-07T22:49:10.527 回答