Article View: comp.std.c++
Article #2722Overloaded new/delete
From: Alan Gray
Date: Sat, 25 Nov 1995 08:55
Date: Sat, 25 Nov 1995 08:55
96 lines
3126 bytes
3126 bytes
Given
class Malloc {
public:
void *allocate(size_t sz);
void deallocate(void *ptr);
};
void *operator new(size_t sz,Malloc *myalloc)
{
return(myalloc->allocate(sz));
}
void operator delete(void *ptr,Malloc *myalloc) // new in WP ??
{
myalloc->deallocate(ptr);
}
class Base {
public:
Base();
virtual ~Base();
};
class Derived : public Base {
public:
Derived();
~Derived();
};
void fred(Malloc *myalloc)
{
double *a = new(myalloc) double[1024];
double *b = new(myalloc) double;
Base *c = new(myalloc) Derived[1024];
Base *d = new(myalloc) Derived;
... lots of code
// HOW do i free a,b,c & d so that
// (1) all the destructors are called
// (2) the memory is deallocated
delete(myalloc) [] a; // not C++
delete(myalloc) b; // not C++
delete(myalloc) [] c; // not C++
delete(myalloc) d; // not C++
// call manually
::operator delete(a,myalloc);
::operator delete(b,myalloc);
// will this call the destructor for the 1024 Derived elements created
// & deallocate all the memory
::operator delete(c,myalloc);
// will this call the destructor for the 1 Derived element created
// & deallocate all the memory
::operator delete(d,myalloc);
}
Other questions this code raises is
(1)
Will size_t sz passed to new(size_t sz,Malloc *mymalloc)
include the overhead for the number of objects allocated ??
and will the compiler adjust the retured pointer so that
a,b,c & d point to the start of array, not at the number of objects ?
(P.S) Microsoft Visual C++ 2.0 works OK here
(2)
How can the compiler known that c was an array of objects allocated,
especially if c is deleted in a different compilation unit.
Is there a specific syntax for this.
I'm guessing but, do I need to declare
void *operator new[](size_t sz,Malloc *myalloc) &
void *operator delete[](size_t sz,Malloc *myalloc)
Alan Gray
4D Solutions PTY LTD
PO BOX 103 Narrabeen NSW
2101 Australia
agray@ozemail.com.au
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<199511250708.SAA29735@oznet02.ozemail.com.au>Path:
arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-backShow Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-back From: "Alan Gray" <agray@oznet02.ozemail.com.au> Subject: Overloaded new/delete Message-ID: <199511250708.SAA29735@oznet02.ozemail.com.au> X-Pmrqc: 1 Originator: fjh@munta.cs.mu.OZ.AU Sender: news@cs.mu.OZ.AU (CS-Usenet) Reply-To: agray@ozemail.com.au Priority: normal Organization: - X-Original-Date: Sat, 25 Nov 1995 18: 08:44 +0000 Date: Sat, 25 Nov 1995 08:55:47 GMT Approved: fjh@cs.mu.oz.au X-Confirm-Reading-To: agray@ozemail.com.au X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMLbaLuEDnX0m9pzZAQF51QF+PhoSqjdVsPhXYX/ZE1hScgkxp7CJ1NMO mbvFPkJEFNS5ujeswPaSPMsE2XXWwCcv =w0/R X-Mailer: Pegasus Mail for Windows (v2.0-WB1) Comments: Authenticated sender is <agray@mail.ozemail.com.au> Return-Receipt-To: agray@ozemail.com.au