3

I am trying to understand overloading new operator. I wrote the code as below.

#include <iostream>
#include <cstdlib>
#include <new>

using namespace std;

class loc
{
    int lo, la;

    public:
        loc()
        {
        }

        loc(int x, int y)
        {
            cout << "In constructor\n";
            lo = x;
            la = y;
        }

        void show()
        {
            cout << lo << " ";
            cout << la << endl;
        }

        void *operator new(size_t sz);
        void operator delete(void *p);
};

void *loc::operator new(size_t sz)
{
    cout << "in Overloaded new\n";
    void *p = malloc(sz);

    return p;
}

void loc::operator delete(void *p)
{
    cout << "in Overloaded delete\n";
    free(p);
}

int main()
{
    loc *p1 = new loc(10, 20);
    p1->show();
    delete p1;
    return 0;
}

I thought it won't call the constructor because I overloaded the new operator with malloc function call inside overloading function. But the output is as below.

in Overloaded new
In constructor
10 20
in Overloaded delete

That means constructor is getting called. How this is possible? Does this mean will malloc() call constructor?

kadina
  • 4,024
  • 3
  • 26
  • 60
  • 2
    Allocating new memory for an object, and constructing the object, are two completely separate processes. Overloading the new operator implements a custom allocator, instead of using the default one that allocates the memory for the object from the heap. It has no effect on the object's construction. – Sam Varshavchik Jun 29 '15 at 01:13
  • 1
    Why wouldn't the constructor get called? `new` gets you memory for the object. The constructor handles building the object (including using your 10 and 20 parameters). – John3136 Jun 29 '15 at 01:14
  • @SamVarshavchik - Does that mean new internally won't call class constructor? – kadina Jun 29 '15 at 01:22

1 Answers1

8

A new expression results in two separate things happening: allocation of the memory needed by the object begin created, and initialization of the object.

The new operator, on the other hand, just handles the allocation part. When you overload the new operator with respect to a specific class, you are replacing the allocation of memory to the object.

This division of functions makes sense when you realize that not all objects are allocated on the heap. Consider the following case:

int main() {
    string someString;
    ..
}

The local variable is not dynamically allocated; new is not used; however the object still needs to be initialized so the constructor is still called. Note that you did not need to explicitly call the constructor - it is implicit in the language that an appropriate constructor will always be called to initialize an object when it is created.

When you write a 'new expression', the compiler knows to emit instructions to invoke the new operator (to allocate memory as needed) and then to call the constructor (to initialize the object). This happens whether or not you overload the new operator.

harmic
  • 22,855
  • 3
  • 52
  • 72
  • new is keyword, not operator – 陳 力 May 22 '18 at 12:35
  • https://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c https://en.cppreference.com/w/cpp/keyword – 陳 力 Oct 29 '18 at 01:08
  • @陳力 'keyword' and 'operator' are not mutually exclusive. See https://en.cppreference.com/w/cpp/language/operator_precedence which lists new as an operator. – harmic Oct 29 '18 at 03:11
  • @Nik-Lz https://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new#comment1786599_1885882 language lawer. – 陳 力 Nov 03 '18 at 02:30
  • https://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new#comment1786879_1885896 Yes, sometimes make such subtle diff is meaningless – 陳 力 Nov 03 '18 at 02:32