By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,118 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,118 IT Pros & Developers. It's quick & easy.

using malloc inside overloaded new operator

P: n/a
Hi,

I am maintaining a C++ project which is a server which continuously
receives requeste from clients.

I have noticed that we overload the new operator and in it then call
malloc to allocate memory.

Specifically the code is something like this.

template <class T>
class CSimpleList
{
public:
CSimpleList()
{
// some code
}
inline void* operator new(size_t tSize){ return malloc(tSize); }
inline void operator delete( void* p ){ free(p); }
};

Is it all right to use it like this. Because i was under the impression
that even after you overload the new operator, it does memory
allocation implicitly(i.e even if you dont do a malloc or some other
kind of memory allocation).
So in the above case, wont memory be allocated twice.

Thanks
Vinu

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Is it all right to use it like this. Because i was under the impression
that even after you overload the new operator, it does memory
allocation implicitly(i.e even if you dont do a malloc or some other
kind of memory allocation).
So in the above case, wont memory be allocated twice.


No, it won't. Operator new does allocation by calling function operator
new(), nothing more, end of story. In fact, in many systems the default
operator new() function does nothing more than just a malloc call.

ben
Jul 23 '05 #2

P: n/a
Hi,

Will the constructor be called if I use a malloc() inside the
overloaded new ?

Jul 23 '05 #3

P: n/a
>
Will the constructor be called if I use a malloc() inside the
overloaded new ?


Yes.

T* i = new T;

is equivalent to:

T* i = (T*) malloc(sizeof(T));
i->T::T(); // pseudo C++ code

ben
Jul 23 '05 #4

P: n/a
benben wrote:

Will the constructor be called if I use a malloc() inside the
overloaded new ?

Yes.

T* i = new T;

is equivalent to:

T* i = (T*) malloc(sizeof(T));
i->T::T(); // pseudo C++ code


No, it is equivalent to

T* i = (T*)operator new(sizeof(T));
i->T::T(); // pseudo C++ code

To the OP: What you overload is the operator new call (forwards to
malloc() most of the times, but may do anything you want, including
printing "hello world" and returning 123), nothing else. The 'new
operator' (as opposed to the operator new) cannot be overloaded.

Jonathan

Jul 23 '05 #5

P: n/a
So Whats the difference between "operator new" and "new operator".

what i understand is "operator new" is just like "operator ==" or any
of the other operators.

So then whats "new operator"

Thanks
Vinu

Jonathan Mcdougall wrote:
benben wrote:

Will the constructor be called if I use a malloc() inside the
overloaded new ?


Yes.

T* i = new T;

is equivalent to:

T* i = (T*) malloc(sizeof(T));
i->T::T(); // pseudo C++ code


No, it is equivalent to

T* i = (T*)operator new(sizeof(T));
i->T::T(); // pseudo C++ code

To the OP: What you overload is the operator new call (forwards to
malloc() most of the times, but may do anything you want, including
printing "hello world" and returning 123), nothing else. The 'new
operator' (as opposed to the operator new) cannot be overloaded.

Jonathan


Jul 23 '05 #6

P: n/a
Don't top-post. Rearragned.
To the OP: What you overload is the operator new call (forwards to
malloc() most of the times, but may do anything you want, including
printing "hello world" and returning 123), nothing else. The 'new
operator' (as opposed to the operator new) cannot be overloaded.
So Whats the difference between "operator new" and "new operator".

what i understand is "operator new" is just like "operator ==" or any
of the other operators.

So then whats "new operator"


See my post in http://tinyurl.com/by9l2.
Jonathan

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.