21

I saw code like this:

void *NewElts = operator new(NewCapacityInBytes); 

And matching call explicitly operator delete is used consequent later.

Why do this instead of:

void *NewElts = new char[NewCapacityInBytes]; 

Why explicit call to operator new and operator delete??

1

4 Answers 4

32

Explicitly calling operator new like that calls the global "raw" operator new. Global operator new returns a raw memory block without calling the object's constructor or any user-defined overloads of new. So basically, global operator new is similar to malloc from C.

So:

// Allocates space for a T, and calls T's constructor, // or calls a user-defined overload of new. // T* v = new T; // Allocates space for N instances of T, and calls T's // constructor on each, or calls a user-defined overload // of new[] // T* v = new T[N]; // Simply returns a raw byte array of `sizeof(T)` bytes. // No constructor is invoked. // void* v = ::operator new(sizeof(T)); 
Sign up to request clarification or add additional context in comments.

7 Comments

@zaharpopov: One example would be implementing a memory pool where you allocate a big chunk once and users of the pool would call functions that create objects within the chunk, instead of using new. Depending on the allocation pattern, this can provide better performance than new and delete.
Ooo. Interesting. I didn't know about this corner of C++.
This answer describes what operator new does, but it doesn't explain why we'd use operator new(N) instead of new char[N]. That was the question, after all.
I would say the difference is style. C++ gives you a lot of ways to do the same thing.
Actually, I should probably edit the answer to explicitly mention new T[N].
|
7

If you write:

T *p = new T; 

That allocates enough memory to hold a T, then constructs the T into it. If you write:

T *p = ::operator new(sizeof(T)); 

That allocates enough memory to hold a T, but doesn't construct the T. One of the times you might see this is when people are also using placement new:

T *p = ::operator new(sizeof(T)); // allocate memory for a T new (p) T; // construct a T into the allocated memory p->~T(); // destroy the T again ::operator delete(p); // deallocate the memory 

Comments

2

If you call operator new(bytesize), then you can delete it using delete, whereas if you allocate via new char[bytesize], then you have to match it using delete[], which is an abomination to be avoided wherever possible. This is most likely the root reason to use it.

2 Comments

If you allocate via global operator new you can deallocate it using global operator delete. But global operator delete won't call the destructor. So if you actually construct an object using placement new after calling global operator new, then global operator delete alone is not sufficient to destroy the object. You need to also invoke the destructor explicitly before deallocating the memory. Essentially, both global operator new and operator delete allow you to decouple storage allocation/deallocation and object initialization/destruction.
@Charles: He didn't placement new anything, and that memory doesn't need destructing. As a form of purely allocating memory, then operator new() is the safest bet because it doesn't need irritating delete[] or unusual free() to deallocate. @Steve: delete[] is an abomination because it's unnecessary. It's not like, when you use operator new and not new[], that the heap magically doesn't need to store the size of the block or anything like that.
1

Use it when you want to allocate a block of "raw" memory and don't want anything constructed in that memory.

There is little practical difference between allocating a block of raw memory and "constructing" an array of chars but using operator new clearly signals your intent to anyone reading the code which is important.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.