views:

201

answers:

6

Why can't it just be regular function calls? New is essentially:

malloc(sizeof(Foo));
Foo::Foo();

while delete is

Foo:~Foo();
free(...);

So why does new/delete end up having it's own syntax rather than being regular functions?

+3  A: 

You can overload operator new and operator delete to provide your own allocation semantics. This is useful when you want to bypass the default heap allocator's behavior. For example if you allocate and deallocate a lot of instances of a small, fixed-size object, you may want to use a pool allocator for its memory management.

Having new and delete as explicit operators like other operators makes this flexibility easier to express using C++'s operator overloading mechanism.

For auto objects on the stack, allocation/constructor call and deallocation/destructor calls basically are transparent as you request. :)

ccmonkey
This sounds deep; but I do not understand. Can you show some code?
anon
@anon my C's a little rusty but essentially if you know that you'll ever need 3 of some object at any one time and new/delete will be called frequently. Then you could do this Foo[3] pool; char[3] available = {0, 1, 2}; char top = 2; Foo allocate() { return pool[available[top--]]; } void deallocate(Foo *foo) { available[++top] = foo - pool; } This gives O(1) allocation (provided you adhere to the rules) as opposed to malloc which has to scan free-lists for available space
Robert Davis
+1  A: 

Why does C++ have separate syntax for greater-than? Why can't it just be a regular function call?

greaterThan(foo, bar);
Anon.
It *will* be a function call if the operator is overloaded, won't it?
dreamlax
Because that would be an unnecessary and pointless deviation from C; and also waste keystrokes.
anon
+2  A: 

'Cause there is no way to provide complie-time type safety with a function (malloc() returns void*, remember). Additionally, C++ tries to eliminate even a slightest chance of allocated but uninitialized objects floating around. And there are objects out there without a default constructor - for these, how would you feed constructor arguments to a function? A function like this would require too much of a special-case handling; easier to promote it to a language feature. Thus operator new.

Seva Alekseyev
malloc itself is type unsafe; but New can be made type safe by wrapping it as: Foo* new(args) { Foo* ans = (Foo*) malloc(sizeof(Foo)); Foo::init(ans, args); }
anon
A: 

Bjarne really liked RAII.... I think separating ~Foo() and the free call this would violate exception safety. Other than that, operator delete() and the clumsiness of this notation are what I can think of...

Robert Karl
+4  A: 

Here's a stab at it:

The new operator calls the operator new() function. Similarly, the delete operator calls the operator delete() function (and similarly for the array versions).

So why is this? Because the user is allowed to override operator new() but not the new operator (which is a keyword). You override operator new() (and delete) to define your own allocator, however, you are not responsible (or allowed to for that matter) for calling appropriate constructors and destructors. These function are called automatically by the compiler when it sees the new keyword.

Without this dichotomy, a user could override the operator new() function, but the compiler would still have to treat this as a special function and call the appropriate constructor(s) for the object(s) being created.

+1  A: 

'new/delete' are keywords in the C++ language (like 'for' and 'while'), whereas malloc/calloc are function calls in the standard C library (like 'printf' and 'sleep'). Very different beasts, more than their similar syntax may let on.

The primary difference is that 'new' and 'delete' trigger additional user code - specifically, constructors and destructors. All malloc does is set aside some memory for you to use. When setting aside memory for a simple plain old data (floats or ints, for example), 'new' and 'malloc' behave very similarly. But when you ask for space for a class, the 'new' keyword sets aside memory and then calls a constructor to initialize that class. Big difference.

Jesse K