This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: operator new[] and operator delete[]


Wesley Smith schrieb:
Hi,
I've overloaded the new, delete, new[] and delete[] operators in a C++
class defined as:

static void * operator new (size_t size);
		static void operator delete (void *p);
		
		static void * operator new[] (size_t size);
		static void operator delete[] (void *p);


I've noticed if I do:


Object *o = new Object[100];

It calls new[] but if I do

delete o;

later it calls delete and therefore isn't symmetrical.  How can I get
delete[] called?

Um... by calling it:


delete[] o;

It is part of the C++ language specification that arrays have to be destroyed with operator delete[]. This comes from the syntactic ambiguity between pointers and arrays. The compiler just cannot know if your o points to a single element of type Object or an array of type Object, so we have to tell him. If you use delete instead of delete[], only the first element gets actually destructed (even though the memory of all elements is freed).

Daniel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]