Posting New and Exceptions

The "new" operator is declared as such:

void* operator new (std::size_t size, void* ptr) noexcept;

However, although this is not related to any actual distribution, so that exceptions for effective exclusion are excluded, it is still possible that the pointer indicates a poor location, in which case one would expect a range or overshot / overshoot error, but would not does what it was declared as noexceptjust stop executing instead?

Also, does this mean that before posting in C ++ 11, new will throw and try to handle std::unexpectedin the event std::set_unexpectedinstead of a direct failure?

Shouldn't the overload of posting a new “just in case” be thrown?

+4
source share
4 answers

The point of using placement syntax is to achieve custom allocation, so standard deallocation is usually required. Therefore, the action taken depends on the dispenser used.

It is your task to provide the correct address for posting a new one for work.

IN RESPECT TO RELATIONS TO COMMENTS: -

#include <new>        // Must #include this to use "placement new"
#include "Fred.h"     // Declaration of class Fred

    void someCode()
    {
      char memory[sizeof(Fred)];     // Line #1     //Allocate enough memory.
      void* place = memory;          // Line #2     // There no need for this.

      Fred* f = new(place) Fred();   // Line #3 (see "NOTE" below)
      // The pointers f and place will be equal

      ...
    }

. , , " ", , . , , . Fred 4- , , , .

, , , , , :)

, .

+4

, , , , : , ( ).

, . operator new.

-:

new int(5)        // non-placement form
new(1,2,3) int(5) // placement-form

, - :

void* address = ...;
::new(address) int(5) // "the" placement-form

, , - .

. , ​​ :

void* operator new (std::size_t size, void* ptr) noexcept;

-op (return ptr;), , . , .

+2

new , , ( , ..). .

, .

: - , , - . , , , .

0

, , - .

. , . , nullptr , .

- UB, . ( : x86)

, , , , , . , C ++ mem.

0

All Articles