| maynard 2006-01-24, 7:07 pm |
| I'm attempting to overload the new[] operator. This is my first
attempt at doing so, but I've found quite a few articles to help me
out. Here is the source for my overloaded operator new[]:
class myClass
{
public:
myClass();
~myClass();
void *operator new[](size_t, long);
protected:
double *v1;
double *v2;
long num_pts;
};
myClass::myClass()
{ //ctor... }
myClass::~myClass()
{ //dtor... }
void *myClass::operator new[](size_t sz, long numpts)
{
sz /= sizeof(myClass);
myClass *x = ::new myClass[sz];
for(size_t j = 0; j < sz; j++){
x[j].v1 = new double[numpts];
x[j].v2 = new double[numpts];
for (long i=0;i<numpts;++i){
x[j].v1[i] = 0.;
x[j].v2[i] = 0.;
}
x[j].num_pts = numpts;
}
return x;
}
I pass the "message" as follows:
int numObjs = 100;
long totalPts = 100000;
myClass* myObj = new(totalPts) myClass[numObjs];
When I step through this in the debugger (MS VC++ .NET), it appears
that the local "x" (in operator new[]) is properly allocated and
initialized. However, when it returns, myObj isn't. I also noticed in
the debugger that the memory location for "x" is 0x00321714, but myObj
(after new[] returns) is 0x00321718.
With that said, two questions...have I properly defined & implemented
my new[] operator? Is there a problem in the debugger??
|