Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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. :)

'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.

'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.

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

greaterThan(foo, bar);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top