5

I know that, new operator will call the constructor of class.

But how it's happening, what is ground level techniques used for this.

Ganesh Kumar
  • 51
  • 1
  • 1
  • 2
  • 3
    See [this answer](http://stackoverflow.com/questions/2697892/what-is-return-type-of-new-in-c/2697929#2697929) to a similar question. Basically, you need to understand that there's a __new expression__ and a __new operator__, which are related, but not the same. A _new expression_ first invokes _operator new_ and then the _constructor_. – sbi May 31 '10 at 09:04

3 Answers3

6

Here is how I imagine it:

T* the_new_operator(args)
{
    void* memory = operator new(sizeof(T));
    T* object;
    try
    {
        object = new(memory) T(args);   // (*)
    }
    catch (...)
    {
        operator delete(memory);
        throw;
    }
    return object;
}

(*) Technically, it's not really calling placement-new, but as long as you don't overload that, the mental model works fine :)

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • This is very simplified and doesn't reflect the fact that when an exception is thrown all *completely constructed* subobjects are run destructors on. Although I can't imagine how you could illustrate that in the preudocode. – sharptooth May 31 '10 at 07:24
  • 3
    @sharp But that's what any constructor always does, regardless of where the memory comes from. It is nothing that the new operator has to do specifically. – fredoverflow May 31 '10 at 07:35
4

It's not really the new operator that calls the constructor. It is more the compiler that translate the following line:

MyClass * mine = new MyClass();

Into the following:

MyClass * mine = malloc(sizeof(MyClass));  // Allocates memory
mine->MyClass();                           // Calls constructor

With other error handling code that other answers have noted.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • 4
    No, not ever `malloc()` - `operator new()` is what is called. Big difference (for example, you can overload `operator new()` per class). – sharptooth May 31 '10 at 07:37
  • True, but I used `malloc()` for the simplicity, and to try to indicate that it is only a matter of allocating a bunch of memory. Nothing else. Overloading `operator new` is more advanced considerations I woudn't use in an explanation for lower level questions as the current. – Didier Trosset May 31 '10 at 07:50
  • 1
    Sadly, the name `malloc` fits the purpose a lot better than `operator new`, because the latter is often confused with the new operator... :( – fredoverflow May 31 '10 at 10:15
1

The compiler generates machine code for that. When the compiler sees

CSomeClass* object = new CSomeClass();

(new statement) it generates code that calls the appropriate operator new() (which allocates memory), calls the right constructor, calls destructors of all fully constructed subobjects in case of exception, calls operator delete() in case an exception occurs during construction. All this is done by extra machine code generated by the C++ compiler for that simply looking statement.

sharptooth
  • 167,383
  • 100
  • 513
  • 979