Operator new overloading

Hi there , I have a few questions :

1) I've read that something like this
 
int *a = new int;


is actually a "new expressions" that calls the "operator new" and converts the void* to the destination type* (int* in this case).
Does the "new expression" also need to be overloaded when overloading the "operator new"?

2) Lets say I've overloaded operator new to take an extra argument :
1
2
3
4
5
struct elem
{
        int a;
	void* operator new (size_t nbytes, int initA);
};

and now I cant use the ordinary "new expression".
 
elem *a = new elem;

Would exposing the default "operator new" solve the problem ? Is there any way to expose it without writing an extra member function that simply calls ::operator new(nBytes) ?

3) Do I have to call the class/struct constructor in the overloaded operator new explicitly (probably by using placement new after malloc) or does it get called implicitly ?

Thanks in advance !
Last edited on
Does the "new expression" also need to be overloaded when overloading the "operator new"?
You cannot change new-expression at all.

and now I cant use the ordinary "new expression".
Your operator new requires additional parameter you didn't provide. So either add another operator new with default signature or provide that parameter explicitely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <new>

struct elem {
    int a;
    void* operator new (size_t nbytes, int initA)
    {
        std::cout << initA << '\n';
        return ::operator new(nbytes);
    }
};

int main()
{
    elem* x = new(4) elem;
}
4



Do I have to call the class/struct constructor in the overloaded operator new explicitly (probably by using placement new after malloc) or does it get called implicitly ?
new-expression does two things:
1) Allocate memory (using either global or member operator new)
2) Create object in allocated memory (same as placement new)
Then writing:

 
elem* p = new elem;


Gets translated by the compiler to :
1
2
elem* p = static_cast<elem*> ( ::operator new(sizeof(elem)) ) ; //or overloaded version
p = new(p) elem(); //placement new 


Did I get this right ? I added the cast as operator new returns void*, as the overloaded versions also should.
Last edited on
Technically, no: it does not call placement new, it is just an action new-expression does, but your code will behave same way as standard implementation does.

Things we call placement new are just version of operator new which are called when new-expression is given additional arguments. Standard version provides non-overloadable void* operator new ( std::size_t count, void* ptr ); and array equivalent which just returns second parameter: no actual cllocation done. You are free to implement another versions of placement-new (or new-with-params)
Ok, so to conclude, let me ask a final question just to make sure I got it right this time ;).

Would this code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <new>

struct elem {
    int a;
    elem() : a(0) {} // added in this post
    void* operator new (size_t nbytes, int initA)
    {
        std::cout << initA << '\n';
        return ::operator new(nbytes);
    }
};

int main()
{
    elem* x = new(4) elem;
}


call the elem constructor and initialize a to 0 or no ?
Last edited on
Yes, it will call constructors. All new-expressions do that.
You could check for yourself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <new>

struct aaa {
    aaa(int i)
    {
        std::cout << "a constructed with " << i << '\n';
    }
};

struct elem {
    aaa a;
    elem() : a(0)
    {
        std::cout << "elem constructed\n";
    }
    void* operator new (size_t nbytes, int initA)
    {
        std::cout << initA << '\n';
        return ::operator new(nbytes);
    }
};

int main()
{
    elem* x = new(4) elem;
}
4
a constructed with 0
elem constructed
Last edited on
OK, thank you for your help :) , I must have been really tired that I didnt think of checking it by myself.
Topic archived. No new replies allowed.