Global distribution functions and const void *

Is the code below represented "undefined behavior" according to C ++ 11 (due to the use of const_cast, see quote below)

const void* p = operator new(123);
operator delete(const_cast<void*>(p));

Corresponding quote from the C ++ 11 standard (3.7.4.2.3):

The value of the first argument provided to the release function may be a null pointer value; if so, and if the release function is included in the standard library, the call has no effect. Otherwise, the behavior will not be determined if the value specified in operator delete(void*)the standard library is not one of the values ​​returned by the previous call either operator new(std::size_t)or operator new(std::size_t, const std::nothrow_t&)in the standard library

If the answer is no, specify quotation marks from the C ++ 11 standard that confirm this.

+4
source share
1 answer

This is not undefined. The rationale is as follows:

  • operator newreturns void*, so it is guaranteed to return modifiable (non-constant) memory: [support.dynamic]

    void* operator new(std::size_t size);
    
  • const_castthat discards a constant is valid if the referenced object is not const: [expr.const.cast] §7, referencing [dcl.type.cv], in particular, §3 + 4

    3 cv cv, , ; const- , .

    4 , , mutable (7.1.1), , (3.8) undefined.

  • const_cast : [expr.const.cast] §3:

    ... const_cast .

+4

Source: https://habr.com/ru/post/1546154/


All Articles