MistyD
MistyD

Reputation: 17223

Overloading a new operator - Shouldn't this code yield an error

I am trying to understand how the operator new can be overloaded and got this from the research I did online. The new operator overload function takes in a size_t type. However in my main I am calling it using new Dummy(). From my understanding Dummy gets implicitly converted to size_t correct ? then what happens to () after Dummy ? shouldn't that result in an error ?

void* operator new(size_t sz)
{
  void* m = malloc(sz);
  std::cout<<"User Defined :: Operator new"<<std::endl;

  return m;
}

class Dummy
{
public:
  Dummy()
  {
    std::cout<<"Dummy :: Constructor"<<std::endl;
  }
  ~Dummy()
  {
    std::cout<<"Dummy :: Destructor"<<std::endl;
  }
};


int main()
{
    Dummy * dummyPtr = new Dummy();
}

Upvotes: 1

Views: 70

Answers (2)

Ivan Sanz Carasa
Ivan Sanz Carasa

Reputation: 1387

There are 2 things involving new keyword:

First you have the new operator, there are different overloads defined by the standard and you can override them or create your new ones. It's responsibility is allocating memory of N size and returning a void* to it. You can even create your own new operator that allocates memory and gets an extra parameter like this:

void* operator new(size_t sz, char x) // x is a placement param
{
    std::cout << "User Defined :: Operator new" << std::endl;
    return ::operator new(sz); // standard new
}

auto* x = new('*') int(234); // our overload

But wait a second, how does the new operator get the size of the type we want to instantiate??

Our friend the new expression just entered the game!

Think about the new expression like some syntactic sugar for calling the new operator. It has this structure:

new(placement_params) type+initializer

it means that for new int(22) the expression will be new(no placements) int + copy_initializer(22) and it will internally call the new operator passing the size of the type.

New operator will return a void* that points to the memory that it just allocated and the right-size of the new expression will initialize that memory. In this example calling the copy initializer

Upvotes: 1

kmdreko
kmdreko

Reputation: 60022

new is a language construct and not a regular function to it has special syntax and conveniences regarding its usage. There's differences between a new expression and operator new

The following both construct a Dummy but one uses local storage and the other uses dynamic storage. So in both cases Dummy() is part of the constructor call and, in the second case, not tied directly to new.

auto d1 = Dummy();
auto d2 = new Dummy();

Here the new expression both allocates space and constructs the Dummy in that space using the supplied constructor and arguments. The amount of space is determined by the compiler via sizeof Dummy.

When you overload the default operator new, it will use your user-defined function to do the allocation, but not the construction (that is done by the new expression).

Upvotes: 0

Related Questions