www.digitalmars.com         C & C++   DMDScript  

c++.beta - Issue with new[] in beta 8.41.2n

reply Sean wall <Sean_member pathlink.com> writes:
Hi Walter,

I tried the latest beta compiler (8.41.2n) on the following program (which is
the 'ArrayOperatorNew.cpp' example from 'Thinking in C++, 2ed' page 574.
================================================

// Operator new for arrays
#include <new> // Size_t definition
#include <fstream>
using namespace std;
ofstream trace("ArrayOperatorNew.out");

class Widget {
static const int sz = 10;
int i[sz];
public:
Widget() { trace << "*"; }
~Widget() { trace << "~"; }
void* operator new(size_t sz) {
trace << "Widget::new: " << sz << " bytes" << endl;
return ::new char[sz];
}
void operator delete(void* p) {
trace << "Widget::delete" << endl;
::delete []p;
}
void* operator new[](size_t sz) {
trace << "Widget::new[]: " << sz << " bytes" << endl;
return ::new char[sz];
}
void operator delete[](void* p) {
trace << "Widget:delete[]" << endl;
::delete []p;
}
};

int main() {
trace << "new Widget" << endl;
Widget* w = new Widget;
trace << "\ndelete Widget" << endl;
delete w;
trace << "\nnew Widget[25]" << endl;
Widget* wa = new Widget[25];
trace << "\ndelete []Widget" << endl;
delete []wa;
}

=======================================================

I compiled with 'dmc -Aa -ND -D_DLL ArrayOperatorNew.cpp'.  It compiles fine,
but there's two issues I wanted to ask about:

1) The generated log file 'ArrayOperatorNew.out' should say:

new Widget
Widget::new: 40 bytes
*
delete Widget
~Widget::delete

new Widget[25]
Widget::new[]: 1004 bytes
*************************
delete []Widget
~~~~~~~~~~~~~~~~~~~~~~~~~Widget:delete[]


However, the call to Widget::new[] is never made, so the line 'Widget::new[]:
1004 bytes' is never printed.  It's as if operator new[] isn't implemented.  Is
that true?

2) This compilation succeeds even if I DON'T use the '-Aa' option.  What exactly
does '-Aa' do?

Thanks!

-Sean
Jun 02 2004
parent "Walter" <newshound digitalmars.com> writes:
-Aa is the default, and is always on.

"Sean wall" <Sean_member pathlink.com> wrote in message
news:c9l8us$2q4a$1 digitaldaemon.com...
 Hi Walter,

 I tried the latest beta compiler (8.41.2n) on the following program (which
is
 the 'ArrayOperatorNew.cpp' example from 'Thinking in C++, 2ed' page 574.
 ================================================

 // Operator new for arrays
 #include <new> // Size_t definition
 #include <fstream>
 using namespace std;
 ofstream trace("ArrayOperatorNew.out");

 class Widget {
 static const int sz = 10;
 int i[sz];
 public:
 Widget() { trace << "*"; }
 ~Widget() { trace << "~"; }
 void* operator new(size_t sz) {
 trace << "Widget::new: " << sz << " bytes" << endl;
 return ::new char[sz];
 }
 void operator delete(void* p) {
 trace << "Widget::delete" << endl;
 ::delete []p;
 }
 void* operator new[](size_t sz) {
 trace << "Widget::new[]: " << sz << " bytes" << endl;
 return ::new char[sz];
 }
 void operator delete[](void* p) {
 trace << "Widget:delete[]" << endl;
 ::delete []p;
 }
 };

 int main() {
 trace << "new Widget" << endl;
 Widget* w = new Widget;
 trace << "\ndelete Widget" << endl;
 delete w;
 trace << "\nnew Widget[25]" << endl;
 Widget* wa = new Widget[25];
 trace << "\ndelete []Widget" << endl;
 delete []wa;
 }

 =======================================================

 I compiled with 'dmc -Aa -ND -D_DLL ArrayOperatorNew.cpp'.  It compiles
fine,
 but there's two issues I wanted to ask about:

 1) The generated log file 'ArrayOperatorNew.out' should say:

 new Widget
 Widget::new: 40 bytes
 *
 delete Widget
 ~Widget::delete

 new Widget[25]
 Widget::new[]: 1004 bytes
 *************************
 delete []Widget
 ~~~~~~~~~~~~~~~~~~~~~~~~~Widget:delete[]


 However, the call to Widget::new[] is never made, so the line
'Widget::new[]:
 1004 bytes' is never printed.  It's as if operator new[] isn't
implemented. Is
 that true?

 2) This compilation succeeds even if I DON'T use the '-Aa' option.  What
exactly
 does '-Aa' do?

 Thanks!

 -Sean
Jul 02 2004