C++ New Library - operator delete[]


Advertisements

Description

It deallocates storage space of array.

Declaration

Following is the declaration for operator delete[].

		
void operator delete[] (void* ptr) throw();    (ordinary delete)
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw();     (ordinary delete)
void operator delete[] (void* ptr, void* voidptr2) throw();            (placement delete)

C++11

			
void operator delete[] (void* ptr) noexcept;	(ordinary delete)
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) noexcept;	 (nothrow delete)
void operator delete[] (void* ptr, void* voidptr2) noexcept;        (placement delete)

C++14

void operator delete[] (void* ptr) noexcept;          (ordinary delete)
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) noexcept;                   (nothrow delete)
void operator delete[] (void* ptr, void* voidptr2) noexcept;               (placement delete)
void operator delete[] (void* ptr, std::size_t size) noexcept;                     (ordinary delete with size)
void operator delete[] (void* ptr, std::size_t size,
                        const std::nothrow_t& nothrow_constant) noexcept;                 (ordinary delete with size)

Parameters

  • size − It contain size in bytes of the requested memory block.

  • nothrow_value − It contains the constant nothrow.

  • ptr − It is a pointer to an already-allocated memory block of the proper size.

  • voidptr2 − It is a void pointer.

Return Value

none

Exceptions

No-throw guarantee − this function never throws exceptions.

Data races

It modifies the storage referenced by the returned value.

Example

In below example explains about new operator delete[].

#include <iostream>

struct MyClass {
   MyClass() {std::cout <<"MyClass is constructed\n";}
   ~MyClass() {std::cout <<"MyClass is destroyed\n";}
};

int main () {
   MyClass * pt;

   pt = new MyClass[3];
   delete[] pt;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

MyClass is constructed
MyClass is constructed
MyClass is constructed
MyClass is destroyed
MyClass is destroyed
MyClass is destroyed
new.htm
Advertisements