www.digitalmars.com         C & C++   DMDScript  

c++ - bug with overloading global operator new()?

reply Sean Wall <Sean_member pathlink.com> writes:
I tried an example from Bruce Eckel's "Thinking in C++" which overloads the
global operator new.  (I'm using 8.38 of DMC++).  Here's a snippet of code from
the example:

..
#include <cstdio>
#include <cstdlib>
using namespace std;

void* operator new(size_t sz) {
printf("operator new: %d Bytes\n", sz);
..
}

void operator delete(void* m) {
printf("operator delete");
..
}

class S {
int i[100];
public:
S() { ... }
~S() { ... }
};

int main() {
int* p = new int(47);
delete p;
S* s = new S;
delete s;
S* sa = new S[3];
delete []sa;
}

For the first two 'new' operators, the overloaded 'new' gets called and I get
the message.  In addition, the overloaded 'delete' gets called to deallocate the
objects.  However, when the array of S[3] objects is allocated, the overloaded
'new' does NOT get called.  An examination of the assembly code shows a call to
a mangled name containing 'vec_new'.  Interestingly, the overloaded 'delete'
DOES get called to deallocate the array.  I've tried this example under g++ and
the overloaded 'new' gets called in all three cases, as Bruce Eckel indicates.
(As a side note, the DMC option '-Aa', which enables new[] and delete[] to be
overloaded for classes, has no effect.)

What is this 'vec_new'?  Does the C++ standard require the overloaded 'new' to
be called in all three cases?  I was able to create a global overloaded operator
called 'operator new[]' which WAS called by DMC in the final case.  I'm
wondering if the DMC compiler sees the last operator as 'operator new[]' and not
'operator new'.  Is it possible the Eckel book (and g++) has it wrong?

-Sean
Jan 28 2004
parent reply Sean Wall <Sean_member pathlink.com> writes:
I decided to dig a bit more on this topic.  After checking 'C++ In A Nutshell'
(the O'Reilly book which covers ISO 14882 standard C++), the explanation there
is 'operator new' is called when allocating single objects and 'operator new[]'
is called by the compiler when allocating arrays of objects.  ('delete' and
'delete[]' operate similarly).  This is true for both global versions of these
functions and member function versions.

So it would seem that DMC's behavior is correct.  Well done Walter!

-Sean


In article <bv8iob$gmu$1 digitaldaemon.com>, Sean Wall says...
I tried an example from Bruce Eckel's "Thinking in C++" which overloads the
global operator new.  (I'm using 8.38 of DMC++).  Here's a snippet of code from
the example:

..
#include <cstdio>
#include <cstdlib>
using namespace std;

void* operator new(size_t sz) {
printf("operator new: %d Bytes\n", sz);
..
}

void operator delete(void* m) {
printf("operator delete");
..
}

class S {
int i[100];
public:
S() { ... }
~S() { ... }
};

int main() {
int* p = new int(47);
delete p;
S* s = new S;
delete s;
S* sa = new S[3];
delete []sa;
}

For the first two 'new' operators, the overloaded 'new' gets called and I get
the message.  In addition, the overloaded 'delete' gets called to deallocate the
objects.  However, when the array of S[3] objects is allocated, the overloaded
'new' does NOT get called.  An examination of the assembly code shows a call to
a mangled name containing 'vec_new'.  Interestingly, the overloaded 'delete'
DOES get called to deallocate the array.  I've tried this example under g++ and
the overloaded 'new' gets called in all three cases, as Bruce Eckel indicates.
(As a side note, the DMC option '-Aa', which enables new[] and delete[] to be
overloaded for classes, has no effect.)

What is this 'vec_new'?  Does the C++ standard require the overloaded 'new' to
be called in all three cases?  I was able to create a global overloaded operator
called 'operator new[]' which WAS called by DMC in the final case.  I'm
wondering if the DMC compiler sees the last operator as 'operator new[]' and not
'operator new'.  Is it possible the Eckel book (and g++) has it wrong?

-Sean

Jan 28 2004
parent "Walter" <walter digitalmars.com> writes:
"Sean Wall" <Sean_member pathlink.com> wrote in message
news:bv8nj5$opq$1 digitaldaemon.com...
 I decided to dig a bit more on this topic.  After checking 'C++ In A

 (the O'Reilly book which covers ISO 14882 standard C++), the explanation

 is 'operator new' is called when allocating single objects and 'operator

 is called by the compiler when allocating arrays of objects.  ('delete'

 'delete[]' operate similarly).  This is true for both global versions of

 functions and member function versions.

 So it would seem that DMC's behavior is correct.  Well done Walter!

Thanks! BTW, Bruce & Chuck helped me get DMC++ to be successful with their book examples.
Feb 11 2004