![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ New::operator delete[]
The C++ New::operator delete[] is a regular function that can be explicitly called like any other function. By using the delete[] operator, an expression first calls the relevant destructors for each element in the array (if they are of a class type), and then it calls an array deallocation function.
A member function with the name operator delete[] exists, performs the array deallocation for a class object. The global function operator delete[] is used in all other situations. Only global array deallocation functions are taken into consideration when the delete[] statement is preceded by the scope operator.
Syntax
Following is the syntax for C++ New::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)
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.
Example 1
Let's look into the following example, where we are going to use the operator delete[] and retriveing the output.
#include <iostream> using namespace std; struct C { C() { cout << "Welcome" << endl; } ~C() { cout << "TP" << endl; } }; int main() { C *p = new C; cout << "pass" << endl; delete[] p; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Welcome pass TP TP TP TP TP ........
Example 2
Let's look into the another scenario, where we are going to use the operator delete[].
#include <iostream> using namespace std; int main() { int *p = new int; cout << "Welcome To World" << endl; delete[] p; return 0; }
Output
On running the above code, it will display the output as shown below −
Welcome To World
Example 3
Considering the following where we are going to check the functioning of operator delete[].
#include <cstdio> #include <cstdlib> #include <new> void operator delete[](void* ptr) noexcept { std::puts("TutorialsPoint"); std::free(ptr); } int main() { int* p2 = new int[10]; delete[] p2; }
Output
when the code gets executed, it will generate the output as shown below −
TutorialsPoint
Example 4
Let's consider another example, where we are going to use operator delete[] and retrive the output.
#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; }
Output
when the code gets executed, it will generate the output as shown below −
MyClass is constructed MyClass is constructed MyClass is constructed MyClass is destroyed MyClass is destroyed MyClass is destroyed