Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using malloc inside overloaded new operator

Reply
Thread Tools

using malloc inside overloaded new operator

 
 
Vinu
Guest
Posts: n/a
 
      07-11-2005
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

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      07-11-2005

> 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


 
Reply With Quote
 
 
 
 
pven
Guest
Posts: n/a
 
      07-11-2005
Hi,

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

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      07-11-2005
>
> 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


 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-11-2005
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

 
Reply With Quote
 
Vinu
Guest
Posts: n/a
 
      07-11-2005
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


 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-11-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in calling an overloaded operator= from inside anotheroverloaded operator= Afshin C++ 7 08-16-2011 12:46 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator= in C++ Afshin C++ 0 08-12-2011 12:19 PM
Problem in calling an overloaded operator= from inside anotheroverloaded operator Afshin C++ 0 08-12-2011 12:01 PM
"overloaded cast operator" and "operator const" John Goche C++ 2 09-04-2006 02:48 PM
overloaded global operator new/new[] and corresponding deletes question Dodo C++ 1 08-26-2003 01:59 PM



Advertisments