Jump to content
  • Advertisement
Sign in to follow this  

More confusion - using overloaded new operator

This topic is 4738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay. I've got the code to work now I'm a little confused as to how they actually work. I've ead through the book and it just seems to assume that the reader knows. Can anyone shed some light as to what is going on. I have a class defined as: foo.h
class PhysicsInfo
{
public:
    
    // Stuff here

private:
    DECLARE_HEAP;
};



foo.cpp
#include "foo.h"

DEFINE_HEAP(PhysicsInfo, "Physics info");



#defines (define lines seperated by \ in actual code):
void * operator new (size_t size);
void * operator new (size_t size, Heap * pHeap);
void operator delete (void * pMem, size_t size); 

void * operator new[] (size_t size);
void * operator new[] (size_t size, Heap * pHeap);
void operator delete[] (void * pMem, size_t size); 

#define DECLARE_HEAP     
public:
         static void * operator new(size_t size);
         static void operator delete(void * p, size_t size); 
private:
         static Heap * s_pHeap; 

#define DEFINE_HEAP(className,heapName)
        Heap * className::s_pHeap = NULL;
        void * className::operator new(size_t size)
        {
         if (s_pHeap==NULL)
              s_pHeap = HeapFactory::CreateHeap(heapName);
         return ::operator new(size, s_pHeap);
        }
        void className::operator delete(void * p, size_t size)
        {        
           ::operator delete(p);
        }

overloaded operator source:
void * operator new (size_t size) 
{
    return operator new (size, HeapFactory::GetDefaultHeap() );
}


void * operator new (size_t size, Heap * pHeap) 
{
    assert(pHeap != NULL);
    return pHeap->Allocate(size);
}


void operator delete (void * pMem) 
{
    if (pMem != NULL)
        Heap::Deallocate (pMem);    
}

void * operator new[] (size_t size)
{
    return operator new[] (size, HeapFactory::GetDefaultHeap() );
}

void * operator new[] (size_t size, Heap * pHeap)
{
    assert(pHeap != NULL);
    return pHeap->Allocate(size);
}

void operator delete[] (void * pMem)
{
    if (pMem != NULL)
        Heap::Deallocate (pMem);
}



The foo class is instansitaed as: foo* pFoo = new foo; Now this seems to work in the test harness that I'm looking at but I'm a little confused as to who the DEFINE_HEAP is called. It isn't in a function or constructor, so is it automatically called when new is called? I'm also concerned about adding this to my existing classes, will I need to ammend the new operators to call the correct constructor and delete to call the correct destructor? Does anyone know?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by garyfletcher
Now this seems to work in the test harness that I'm looking at but I'm a little confused as to who the DEFINE_HEAP is called. It isn't in a function or constructor, so is it automatically called when new is called?


I, as you declare new/delete operators for a class the compiler automatically uses those ones instead of the default global ones.

Quote:
Original post by garyfletcher
I'm also concerned about adding this to my existing classes, will I need to ammend the new operators to call the correct constructor and delete to call the correct destructor?


You don't need to invoke constructors/destructors when redefining/overloading operators new/delete, when implicitly invoked the constructor/destructor is automatically invoked, after allocation and before deallocation.

Its only when you explicitly invoke operators new/delete is when memory is only allocated/de-allocated and the allocated memory is uninitialized (not constructed), in order to initialize them later you would use placement new operator and then explicitly invoke the destructor (for user-defined types) e.g.


#include <new> //placement new

struct foo { /* ... */ };

//....

void* buf = ::operator new(sizeof(foo)); //allocate only

foo* f = ::new(buf) foo(); // construct only, placement new

f->~foo(); // destroy only

::operator delete(buf); // de-allocate only


but you don't need to do this if your just redefining/overloading operators new/delete.

Also note that you don't have to keep overloading operators new/delete for classes, you could do it just once and other class inherit from that class, the compiler automatically gives you the correct size to allocate the derived classes (they could be larger instances).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!