NoFieldsClass::operator new(std::size_t):标准是否允许它每次被调用时 return 相同的地址?
NoFieldsClass::operator new(std::size_t): does the standard allow for it to return the same address each time it's invoked?
作为优化策略的一部分,我想 "pretend" 我实际上是在堆上分配一个对象,而实际上我只是重用一个预先存在的对象,而没有其余的应用程序曾经注意到,因为没有对返回的地址执行任何检查,并且永远不需要访问对象中的任何字段。除此之外,class 构造函数没有副作用。
因此,我想知道根据标准,以下程序是否表现出定义明确、特定于实现、未定义或完全无效的行为。
#include <memory>
#include <cassert>
class Base {
public:
Base(){}
virtual ~Base(){}
};
class Derived: public Base {
public:
using Base::Base;
static void *operator new(std::size_t s) {
static void *mem = ::operator new(s);
return mem;
}
static void operator delete(void *mem) {
/* no-op */
}
};
int main() {
using Ptr = std::unique_ptr<Base>;
Ptr p1 { new Derived };
Ptr p2 { new Derived };
Ptr p3 { new Derived };
Ptr p4 { new Derived };
// assert just in this test, not in the real program
assert(p1 == p2 && p2 == p3 && p3 == p4);
return 0;
}
阅读当前C++工作草案(N4835)的§6.7.2.9似乎无效:
Two objects with overlapping lifetimes that are not bit-fields may have the same address if
one is nested within the other, or if at least one is a subobject of zero size and they are of different types; otherwise, they have distinct addresses and occupy disjoint bytes of storage ²⁹.
但是,上一段所指的注释 29 指出:
Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference.
如开头所述,在我的例子中,程序不太关心分配此对象的地址,所需要的只是可以用 operator new
分配并删除与 operator delete
,所以它似乎符合注释 29 的要求。这是正确的吗?
[basic.stc.dynamic.allocation]/2 The allocation function attempts to allocate the requested amount of storage... 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
.
强调我的。
作为优化策略的一部分,我想 "pretend" 我实际上是在堆上分配一个对象,而实际上我只是重用一个预先存在的对象,而没有其余的应用程序曾经注意到,因为没有对返回的地址执行任何检查,并且永远不需要访问对象中的任何字段。除此之外,class 构造函数没有副作用。
因此,我想知道根据标准,以下程序是否表现出定义明确、特定于实现、未定义或完全无效的行为。
#include <memory>
#include <cassert>
class Base {
public:
Base(){}
virtual ~Base(){}
};
class Derived: public Base {
public:
using Base::Base;
static void *operator new(std::size_t s) {
static void *mem = ::operator new(s);
return mem;
}
static void operator delete(void *mem) {
/* no-op */
}
};
int main() {
using Ptr = std::unique_ptr<Base>;
Ptr p1 { new Derived };
Ptr p2 { new Derived };
Ptr p3 { new Derived };
Ptr p4 { new Derived };
// assert just in this test, not in the real program
assert(p1 == p2 && p2 == p3 && p3 == p4);
return 0;
}
阅读当前C++工作草案(N4835)的§6.7.2.9似乎无效:
Two objects with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a subobject of zero size and they are of different types; otherwise, they have distinct addresses and occupy disjoint bytes of storage ²⁹.
但是,上一段所指的注释 29 指出:
Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference.
如开头所述,在我的例子中,程序不太关心分配此对象的地址,所需要的只是可以用 operator new
分配并删除与 operator delete
,所以它似乎符合注释 29 的要求。这是正确的吗?
[basic.stc.dynamic.allocation]/2 The allocation function attempts to allocate the requested amount of storage... If the request succeeds, the value returned shall be a non-null pointer value (7.11)
p0
different from any previously returned valuep1
, unless that valuep1
was subsequently passed to anoperator delete
.
强调我的。