3

What is the use or what is the reason for new() and delete() to be implemented as operators in c++ ? What are the advantages of making it an operator instead of a function?

Scooter
  • 6,802
  • 8
  • 41
  • 64
Laavaa
  • 575
  • 9
  • 19

2 Answers2

7

The new operator cannot be a function because it accepts a type as an argument. You cannot write new foo_type as a function call, because foo_type is not an expression that produces a value, but a type name.

The delete operator could be a function that is overloaded for different pointer types, and an extra optional bool argument for delete [] semantics. Why there is a delete operator is probably for symmetry with its new counterpart.

That said, template functions can take types as template arguments. But the new and delete operators historically precede templates.

Also, a function can be written which takes, instead of a type, a prototype instance of a type, from which an object is constructed: newobj(m_class(constructor_arg)). This newobj is overloaded for different types. It allocates space, copy constructs into it, and returns the object.

So in the end, the design reflects the tastes and whims of the designer.

The separation itself between operators and functions (and statements, declarations, and so on) is not strictly necessary in language design.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • 2
    You can't write `new foo_type` as a function call because that's not valid syntax for function calls (even if the argument were a value instead of a type). However you could easily have a function call like `new()`. The only problem with that is that the `new` operator was introduced before templates were. If templates had been introduced first, I can easily imagine `new` having been defined as a template function. – sepp2k Nov 20 '12 at 16:47
  • 2
    More specifically, having a type parameter means that you can selectively override the functionality of `new` throughout the application. You cannot do the same with `malloc`; instead you'd have to write a new allocator function and then modify chunks of your code to use it. – Rook Nov 20 '12 at 16:51
  • 3
    @sepp2k it probably wouldn't have been because in addition to a type new can also take an initializer. Until variadic templates that wouldn't have been possible, and even variadic templates with perfect forwarding aren't a perfect replacement for that. – bames53 Nov 20 '12 at 16:52
  • 1
    The sentence "you can't write new foo_type as a function call" is meant to be interpreted by humans who understand that writing something as a function call means transliterating it into the appropriate syntax. It doesn't mean that `new foo_type` isn't a C++ function call syntactically, which is self-evident. – Kaz Nov 20 '12 at 16:56
  • @bames53: Good point when referring to the *new-expression*, but not when talking about the *new-operator*. The former being what you use to create an object, the latter being how you override allocation of memory. – David Rodríguez - dribeas Nov 20 '12 at 16:57
  • @Kaz: Same thing as my comment to bames53, it seems that you are confusing the new operator (allocation) with the new expression (creation of objects). The new operator does not take a type, but a size and potentially other arguments (as `std::nothrow`) – David Rodríguez - dribeas Nov 20 '12 at 17:01
  • @Kaz Except that you CAN write it as a function call by transliterating it into the appropriate syntax. – sepp2k Nov 20 '12 at 17:03
  • 1
    @DavidRodríguez-dribeas typically "`new` operator" refers to the keyword `new` used as an operator while "`operator new`" is used to refer to the allocation function. Also `operator new` doesn't take a type... – bames53 Nov 20 '12 at 17:10
  • 1
    @DavidRodríguez-dribeas I am not confusing anything with anything. – Kaz Nov 20 '12 at 17:31
  • @DavidRodríguez-dribeas No, you're talking about `operator new`, the `new` operator *does* take a type. – Christian Rau Nov 20 '12 at 20:12
3

There are two common reasons that I know of that people override them.

1) Use new/delete to keep track of a pool of objects. New grabs the first unused resource from the pool, delete returns that resource to the pool. This is useful if you're constantly allocating/deallocating objects.

2) Tracking memory usage. You can track down leaks or memory overwriting issues. You could pad the memory on either side for instance, or keep track of who is allocating memory so you can track who didn't deallocate it.

Ryan Guthrie
  • 688
  • 3
  • 11