Hello everyone,
I remembered delete[] is implemented through operator overloading, but I am not quite clear.
Could anyone recommend some links about how delete[] is implemented so that I can learn and refresh my memory? :-)
thanks in advance,
George
This is a discussion on delete [] within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I remembered delete[] is implemented through operator overloading, but I am not quite clear. Could anyone recommend some ...
Hello everyone,
I remembered delete[] is implemented through operator overloading, but I am not quite clear.
Could anyone recommend some links about how delete[] is implemented so that I can learn and refresh my memory? :-)
thanks in advance,
George
I'm pretty sure this isn't "correct". delete [] is a compiler defined function, just like delete "without []".
Put another way:
operator delete()
and
operator delete[]()
are distinct different operator overloads.
Sure, the internal mechanisms are (near enough) identical for the typical case, but they are still different functions in the compiler itself. They are not constructed in some header file by some magical template or operator overload.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The new, new[], delete and delete[] operators consist of two parts: memory management and lifetime management. The two forms of new allocate memory, then initialize the objects. The two forms of delete destruct the objects and then free the memory.
It's the memory management part that you can overload.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
To understand what happens with operator delete, it is necessary to understand what happens with operator new. I'll describe that first....
The operation "p = new Thing[n];" does the following;
1) if Thing has supplied an operator new[](), then p will be assigned the return value from Thing::operator new[](n). Otherwise, p will be assigned the return value from ::operator new[](n);
2) Thing's default constructor will be invoked for each element of the array (n times).
The behaviour of "delete [] p;" is exactly the reverse.
1) Invoke destructors (~Thing())for each element of the array (n times), in reverse of the order that constructors where invoked when the array was created.
2) if Thing has supplied a suitable operator delete [], then invoke Thing::operator delete [](p); otherwise, invoke ::operator delete[](p).
The global operator new[] and global operator delete [] are supplied by the implementation (compiler/library), but can be redefined by the program, and are functions of the form;
Individual classes can also supply their own operator new [] and delete [], of the form;Code:void *::operator new(std::size_t n) throw(std::bad_alloc); void ::operator delete [](void *) throw();
There are a number of other variants, as there are multiple forms of new and delete (single element, array, and placement).Code:class Thing { public: void *operator new[](std::size_t) throw(std::bad_alloc); void operator delete [] (void *) throw(); };
But note that inherited operators new functions are included in that, i.e.1) if Thing has supplied an operator new[](),
Code:class A { public: void *operator new[](std::size_t sz) throw(std::bad_alloc) { ... } void operator delete[](void *) throw() { ... } }; class B : public A { }; B *p = new B[100]; // A::operator new[] is used.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law