1

What are the placement and non-placement allocation/deallocation function? I've been reading sec. 3.7.4.2 of N3797 and come across with the placement and non-placement allocation/deallocation function concepts. For instance:

The global operator delete with exactly one parameter is a usual (non-placement) deallocation function.

I could not find a definition of these concepts and I assume that non-placement is the functions which have one of the following signature:

void* operator new(std::size_t);
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
void operator delete(void*, std::size_t) noexcept;
void operator delete[](void*, std::size_t) noexcept;

Have I understood correctly?

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • 1
    They let you construct object on already allocated memory. vector uses it. – Neil Kirk Jul 24 '14 at 13:28
  • possible duplicate of [What uses are there for "placement new"?](http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new) – Neil Kirk Jul 24 '14 at 13:29
  • @NeilKirk Why do you think so? That is, could you provide a reference to the Standard? –  Jul 24 '14 at 13:29
  • No. I don't know if it's mandatory to do it that way, but I know many implementations do. – Neil Kirk Jul 24 '14 at 13:29
  • I think the definition for *placement deallocation function* is in 3.7.4.2/2, which describes *deallocation functions* and which of those are *non-placement deallocation functions*. I think you can conclude that those *deallocation functions* which are not *non-placement deallocation functions* then are *placement deallocation functions*. – dyp Jul 24 '14 at 13:45
  • @Niall 18.6.1.3 describes the Standard Library's default implementation for some placement (de)allocation functions. It does not define what a placement (de)allocation function is. – dyp Jul 24 '14 at 13:48
  • @DmitryFucintv, my typo.. 18.6.1.3 (n3797) is "Placement forms"... – Niall Jul 24 '14 at 13:48
  • See also the sample code I posted [in this other question on the same section](http://stackoverflow.com/questions/24908550/deallocation-function-precludes-use-of-an-allocation-with-two-parameter). – Khouri Giordano Jul 24 '14 at 13:51
  • @dyp That is, have we already defined placement new and delete form in the standard library? –  Jul 24 '14 at 13:55
  • The Standard Library provides default versions for *some* placement allocation and placement deallocation functions. The "placement" is misleading, it just means that there might be additional parameters. So there's not *a single* placement form. – dyp Jul 24 '14 at 13:59
  • possible duplicate of [Operator delete signature unexpected behavior](http://stackoverflow.com/questions/24770119/operator-delete-signature-unexpected-behavior) – Deduplicator Jul 24 '14 at 14:03

2 Answers2

0

Unfortunately, the standard does not define what it means to be a "placement" allocation or deallocation function.

This is CWG 2592. I think the intended meaning is probably something similar to what is in the proposed resolution.

So the usual allocation functions would be the ones with the following signatures:

void* operator new(std::size_t);
void* operator new(std::size_t, std::align_val_t);
void* operator new[](std::size_t);
void* operator new[](std::size_t, std::align_val_t);

including any replacements declared by the user, and any functions with such signatures at class scope. And the placement ones would be ones with any other signature, including the following from the standard library:

void* operator new(std::size_t, const std::nothrow_t&) noexcept;
void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
void* operator new(std::size_t, void*) noexcept;
void* operator new[](std::size_t, const std::nothrow_t&) noexcept;
void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
void* operator new[](std::size_t, void*) noexcept;

"Usual deallocation function" was given an actual definition in the standard at some point. In N4944 it reads:

A usual deallocation function is a deallocation function whose parameters after the first are

  • optionally, a parameter of type std::destroying_delete_t, then
  • optionally, a parameter of type std::size_t, then
  • optionally, a parameter of type std::align_val_t.

[...] A template instance is never a usual deallocation function, regardless of its signature.

A placement deallocation function is obviously a deallocation function that's not a usual deallocation function; CWG 2592 seeks to clarify this normatively.

So there are 8 possible signatures for a usual deallocation function. A usual deallocation function can have class scope. The following deallocation functions from the standard library are usual deallocation functions:

void operator delete(void*) noexcept;
void operator delete(void*, std::size_t) noexcept;
void operator delete(void*, std::align_val_t) noexcept;
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
void operator delete[](void*) noexcept;
void operator delete[](void*, std::size_t) noexcept;
void operator delete[](void*, std::align_val_t) noexcept;
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;

and the following are placement deallocation functions:

void operator delete(void*, const std::nothrow_t&) noexcept;
void operator delete(void*, std::align_val_t, const std::nothrow_t&) noexcept;
void operator delete(void*, void*);
void operator delete[](void*, const std::nothrow_t&) noexcept;
void operator delete[](void*, std::align_val_t, const std::nothrow_t&) noexcept;
void operator delete[](void*, void*);
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
-1

The definition of placement new is in N3797 in section 5.3.4.13:

The new-placement syntax is used to supply additional arguments to an allocation function. If used, overload resolution is performed on a function call created by assembling an argument list consisting of the amount of space requested (the first argument) and the expressions in the new-placement part of the new-expression (the second and succeeding arguments). The first of these arguments has type std::size_t and the remaining arguments have the corresponding types of the expressions in the new-placement.

Kiroxas
  • 891
  • 10
  • 24
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 1
    A *new-expression* is not an allocation function. Those concepts are linked, but not identical. – dyp Jul 24 '14 at 13:43