www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

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

↑ ↓ ← 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
↑ ↓ 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
↑ ↓ → "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