Thread overview |
---|
January 28, 2004 bug with overloading global operator new()? | ||||
---|---|---|---|---|
| ||||
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 |
January 28, 2004 Resolved? Re: bug with overloading global operator new()? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Wall | 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 > > > |
February 11, 2004 Re: Resolved? Re: bug with overloading global operator new()? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Wall | "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 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! Thanks! BTW, Bruce & Chuck helped me get DMC++ to be successful with their book examples. |
Copyright © 1999-2021 by the D Language Foundation