🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1683

overloaded new/delete

#1683
From: Alan Gray
Date: Wed, 09 Aug 1995 07:09
112 lines
2742 bytes
I pose the following problem.

Given a personalised allocator

    class Malloc {

    public:

      Malloc();

      void *allocate(size_t sz);
      void  deallocate(void *ptr);
    };

    void *operator new(size_t sz,Malloc *m=0)
    // ------------------------------------------------
    // here sz == sizeof(class/struct)
    // ------------------------------------------------
    {
      if(m) {
        return(m->allocate(sz));
      } else {
        return(::operator new(sz));
      }
    }
    void *operator new[](size_t sz,Malloc *m=0)
    {
      if(m) {
        return(m->allocate(sz));
      } else {
        return(::operator new(sz));
      }
    }

and then use Malloc to allocate a series of objects,
then finally delete the objects, how can this be achieved.
The draft standard does not cover this.

    class X {
    public:
      X();
      virtual ~X();
    };
    class Y : public X {
    public:
      Y();
     ~Y();
    };

    void fred(Malloc *m)
    {
      double *xarray = new(m) double[1024];
      double *yarray = new(m) double[1024];
      double *zvalue = new(m) double;
      X      *xone   = new(m) Y[1024];
      X      *xtwo   = new(m) Y;
      ....
      ....


      delete [] xarray;   // eek - gets the wrong version of ::operator 
delete(size_t sz)
      delete [] yarray;   // dito
      delete    zvalue;   // dito
      delete [] xone;     // dito
      delete    xtwo;     // dito
    }

do we need to be able to overload as follows

    void operator delete(void *ptr,Malloc *m=0)
    {
      if(m) {
        m->deallocate(ptr);
      } else {
        ::operator delete(ptr));
      }
    }
    void operator delete[](void *ptr,Malloc *m=0)
    {
      if(m) {
        m->deallocate(ptr);
      } else {
        ::operator delete(ptr));
      }
    }

then fred will look like

    void fred(Malloc *m)
    {
      double *xarray = new(m) double[1024];
      double *yarray = new(m) double[1024];
      double *zvalue = new(m) double;
      X      *xone   = new(m) Y[1024];
      X      *xtwo   = new(m) Y;
      ....
      ....


      delete(m) [] xarray;
      delete(m) [] yarray;
      delete(m)    zvalue;
      delete(m) [] xone;
      delete(m)    xtwo;
    }


Alan Gray
4D Solutions
PO BOX 103 Narrabeen NSW 201
Australia
Technical Details
Message-ID: <409muv$nta@oznet03.ozemail.com.au>
Path: arc1-ca.pugleaf.net!news.mel.aone.net.au!OzEmail!usenet

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mel.aone.net.au!OzEmail!usenet
From: agray@ozemail.com.au (Alan Gray)
Newsgroups: comp.std.c++
Subject: overloaded new/delete
Date: 9 Aug 1995 07:09:19 GMT
Organization: 4D Solutions
Message-ID: <409muv$nta@oznet03.ozemail.com.au>
NNTP-Posting-Host: slsyd2p31.ozemail.com.au
Mime-Version: 1.0
X-Newsreader: WinVN 0.99.3