[cfe-dev] Dynamic memory allocation and deleted definitions
David Blaikie
dblaikie at gmail.com
Tue Nov 26 11:13:07 PST 2013
On Tue, Nov 26, 2013 at 10:53 AM, Rahul Jain <1989.rahuljain at gmail.com> wrote:
> I think I got something relevant: Do these words make any sense in our
> context?
Not quite
> An allocation or deallocation function
Allocation/deallocation function does not refer to
constructor/destructor. See 3.4p1:
"A C++ implementation provides access to, and management of, dynamic
storage via the global allocation functions operator new and operator
new[] and the global deallocation functions operator delete and
operator delete[]."
These are actually not the ctor/dtor, in 3.4p2 their declarations are shown as:
void* operator new(std::size_t);
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
So, roughly, if you say "new T" first operator new(sizeof(T)) is
called, then the ctor for T is called on the returned storage, if the
T ctor throws, then operator delete is called on the storage before
the exception is propagated (that's why even non-array "new T"
odr-uses the deallocation function, operator delete - even though it
doesn't/shouldn't odr-use T's dtor).
> for a class is odr-used by a
> new expression appearing in a potentially-evaluated expression as specified
> in 5.3.4(New) and 12.5(Free store). A deallocation function for a class is
> odr-used by a delete expression appearing in a potentially-evaluated
> expression as specified in 5.3.5(Delete) and 12.5(Free store).
>
> What exactly does one mean by saying that something is odr-used?
The simplistic interpretation is that if something is odr-used it must
be defined (ie: there must be (one) definition). For example:
void f1();
void f2();
int main() {
f1(); // odr use of f1 - the program is ill-formed if 'f1' is not defined
size_t s = sizeof(f2); // non-odr-use of f2 - the program is valid
even if 'f2' is never defined
}
More information about the cfe-dev
mailing list