Example taken partially from Wikipedia

std::string *mas = reinterpret_cast<std::string*>(new char[10 * sizeof(std::string)]); delete[] mas; 

It does not matter reinterpret_cast (static or another) error BLOCK_TYPE_IS_VALID takes BLOCK_TYPE_IS_VALID . The same behavior with operator new[] (10*sizeof(std::string)) .

How to allocate dynamic data byte-by-bye? So I understand the error in alignment? Or in the wrong direction I dig.

 operator delete[] (mas); 

It is necessary to delete correctly. Time later stupid. So normal behavior. Experts what's the problem for the usual delete[] ?

  • For STL, you need to dig in the direction of the allocators. - cpp_user
  • What do you want to do? - Cerbo
  • @Cerbo stepic.org/lesson/ ... I decided through regular pointers. many stars))). I decide now through Wikipedia through clever things, let's say - Artem
  • @Cerbo python + java kill the thoughts of the old decent code. with & c ++ for the soul, pampering. I decided to remember, I found myself free (my_memory) in my mind. I passed BC before the era)) but my soul asks) - Artyom
  • 2
    And where did the placement new come from, where is it in your question? - VladD

2 answers 2

In the same place [on a steppe] in a lecture it seems to be intelligibly told ...

And so, yes, the operation new / new[] consists of two actions: the actual allocation of memory and the call of the constructor. These two actions can be divided by calling the operator new (size_t size) / operator new[] (size_t size) separately - for memory allocation and, in fact, placement new ( new (ptr) Type(ctor_args...) ) to call the constructor and create an object.

Actually then manually have to destroy the object. Those. call the destructor manually:

 ptr->~Type(); 

Then clear the memory with operator delete (ptr)/operator delete[] (ptr)

When you mix placement new and destruction with delete, you yourself are trying to shoot in the foot. Exactly as in the opposite direction.

For example, when calling new[] , memory can be allocated more than requested (we will not go into the nuances of the work of allocators) and objects are not constructed from the beginning of the memory block, but from some indentation and the pointer value is returned to this indent. And in the reserved place is saved including. the number of objects in the array: this information can then be used delete[] to correctly understand how many destructors need to be called. How this will be implemented in a specific compiler is a matter of implementation. But therefore it is impossible to interfere with new / delete[] and new[] / delete : the form of the operator helps the compiler to draw correct conclusions about the structure of the memory. For the same reason, placement new and delete / delete[] cannot be interfered with - in general, the host new can be called for a memory block that has been allocated in some user way: from some system area, from a pool, from some custom allocator etc. delete after destroying the object also tries to free it. What will happen - only Cthulhu is known. And delete[] can still suggest some kind of its structure and start operating with an invalid pointer.

In general - be symmetrical.

    The new operator not only allocates memory, but also calls the constructor or constructors for the objects created or created (in the case of an array).

    When the operator is called delete , the destructors of these objects are called respectively.

    However, in your code snippet, the constructor of the class std::string not called anywhere. Therefore, the allocated memory is not initialized properly. As a result, the delete operator has undefined behavior.

    • and operator delete [] ... does not cause it turns out in this case? - Artem
    • explicit Array (size_t size = 0, const T & value = T ()) {mas = reinterpret_cast <T *> (operator new [] (size * sizeof (T))); this-> size_ = size; for (size_t i = 0; i <size; ++ i) {new (mas + i) T (value); }} and the question isn't the constructor being invoked in this case? // solution of the designer for the templates - Artyom
    • it was worth taking the wikipedia code completely, you cut out the placement itself from there. operator delete[] does not call destructor, delete calls. - sercxjo
    • @sercxjo operator behavior and was interesting. I agree with you, in due time I was told I took Nyurka for a lyashka, then for a lyashka and let go (a rough example), and when I took for a lyashka, and released for a boob I received a pointer on the back of my head))) and that's why it is impossible ??? probably this is C ++ - Artem
    • It is impossible because the destructor is designed for the fact that the constructor was previously called. This example in Wikipedia is not for beginners, but for special cases. If you want to place the data not in the usual dynamic memory but in a predetermined place or in some other memory block (for example, in the memory mapped to a file). In this case, the memory is first allocated, then the designers manually call it for the loop using the special form new with an additional argument. And further it is shown how to release everything back in this case. - sercxjo