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?
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?
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. :)
Why does C++ have separate syntax for greater-than? Why can't it just be a regular function call?
greaterThan(foo, bar);
'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.
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...
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.
'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.