Article View: comp.std.c++
Article #2172overloading global new and delete
From: Alfredo V. Mendoza
Date: Fri, 13 Oct 1995 02:00
Date: Fri, 13 Oct 1995 02:00
153 lines
4552 bytes
4552 bytes
Hello all,
The program below was a testcase sent to IBM compiler defect group.
The problem is when compiled with IBM (413) xlC 3.1.2 , and ran
the result is that the str() function in ostrstream does NOT use
the overloaded new but uses the IBM runtime new. And when the class
is destroyed the memory that was allocated by str() is deleted by the
user defined global delete. Which (IMHO) is wrong.
IBM compiler development claims that when one uses their runtime
classes, it will always use the IBM runtime new and delete regardless
of an overloaded global new and delete.
Unfortunately as this program shows if we follow the logic above
a mismatch will occur.
This program behaves differently from SUN solaris.
I have searched different C++ references and standards books but
I could not find anything that will tell/show how overloading
new and delete globally should be implemented. Hence I cannot
show IBM that their implementation is wrong.
QUESTION:
1. Does anyone know if there are any new standards that can point
to a correct implementation?
the output I get is :
****************************************
new(148) : signpost = 0x20211bb8 : user = 0x20211bbc
str() = 0x20211c58
dtor(.1.) : 0x20211c58
delete() : ** error - no deadbeef signpost @ 0x20211c58
dtor(.2.) : _pointer deleted
****************************************
//
// This test derives a class from ostrstream. It further overloads global
// "new"
// and "delete" such that the "new" allocates 4 bytes more then the requested
// amount of memory. The new uses the extra 4 bytes as a header at the
// beginning
// of the new'ed memory, puts 0xdeadbeef as a sign post in the 4 bytes, and
// returns a pointer to the user after the 4 byte sign post. The "delete"
// operator checks for sign post before free'ing the space. If the sign post
// does not exists, a message is printed out.
//
#include <stdio.h>
#include <stdlib.h>
#include <strstream.h>
// global new operator
void *operator new(size_t size);
// global delete operator
void operator delete(void *p);
class DiagOutput: public ostrstream
{
public:
char * _pointer;
public:
DiagOutput() : _pointer(0) {}
~DiagOutput();
char *safe_str();
};
DiagOutput::~DiagOutput()
{
if (_pointer) {
cout << "dtor(.1.) : " << (void*)_pointer << endl;
delete[] _pointer;
_pointer = 0;
cout << "dtor(.2.) : _pointer deleted" << endl;
}
}
// calls str() to return a pointer to the dynamically allocated
// str buffer
//
char *DiagOutput::safe_str()
{
if (!_pointer) {
_pointer = str();
cout << "str() = " << (void*)_pointer << endl;
}
return _pointer;
}
// overload global delete
//
void operator delete(void *p)
{
if (p) {
unsigned long *p1 = (unsigned long*)p;
p1--; // goto signpost
if (0xdeadbeef != *p1) {
cout << "delete() : ** error - no deadbeef signpost @ " << (void*) p << endl
;
}
else {
free(p1);
}
}
}
// overload global new
//
void* operator new(size_t size)
{
void *p = malloc(size+sizeof(unsigned long));
unsigned long *p1 = (unsigned long*)p; // get pointer to signpost
*p1 = 0xdeadbeef; // set the signpost
p1++; // set to start of user memory
cout << "new(" << size << ") : signpost = " << (void*)p
<< " : user = " << (void*)p1 << endl;
return((void*)p1);
}
main()
{
cout << "****************************************" << endl;
DiagOutput *log = new DiagOutput;
*log << "String1" << " String2" << endl << ends;
char *ss = log->safe_str();
delete log;
cout << "****************************************" << endl;
}
--
Freddie Mendoza
FVM Consulting International
Internet: avm@satori.com
avm@zilker.net
---
[ 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:
<45js9r$iuq@hackberry.zilker.net>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 13 Oct 95 02:00:21 GMT Approved: fjh@cs.mu.oz.au From: avm@zilker.net (Alfredo V. Mendoza) Newsgroups: comp.std.c++ Subject: overloading global new and delete X-Original-Date: 12 Oct 1995 20:03:06 GMT Organization: Zilker Internet Park, Inc. Message-ID: <45js9r$iuq@hackberry.zilker.net> X-Newsreader: TIN [version 1.2 PL2] X-Auth: PGPMoose V1.0 PGP comp.std.c++ iQBFAgUBMH3IQ+EDnX0m9pzZAQEq3QF+MZ3NZJHC0CRo9pqbhS+Cg5LcFZOfwsNr s5yNOzrT20eh3peL37ZSMjExZ8zqOP5b =vRJC