using malloc inside overloaded new operator

Discussion in 'C++' started by Vinu, Jul 11, 2005.

  1. Vinu

    Vinu Guest

    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
    Vinu, Jul 11, 2005
    #1
    1. Advertising

  2. Vinu

    benben Guest


    > 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
    benben, Jul 11, 2005
    #2
    1. Advertising

  3. Vinu

    pven Guest

    Hi,

    Will the constructor be called if I use a malloc() inside the
    overloaded new ?
    pven, Jul 11, 2005
    #3
  4. Vinu

    benben Guest

    >
    > 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
    benben, Jul 11, 2005
    #4
  5. 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
    Jonathan Mcdougall, Jul 11, 2005
    #5
  6. Vinu

    Vinu Guest

    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
    Vinu, Jul 11, 2005
    #6
  7. 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
    Jonathan Mcdougall, Jul 12, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dodo
    Replies:
    1
    Views:
    356
    Ron Natalie
    Aug 26, 2003
  2. John Goche
    Replies:
    2
    Views:
    338
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    286
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    289
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    419
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page