Mail Archives: djgpp/2000/08/17/13:15:28.1
Manni Heumann <manni DOT heumann AT gmx DOT de> wrote
> >What is difference between delete and delete[] ?
> >With one should I use to delete array : char *ptr = new char[1024] ?
> >When I'm overloading operator delete for some class, should I overload
> >operator delete[] too ?
> No offense, but someone, who is just learning how to use
> delete, should not try to overload delete.
I now that my question was wery simple, but I hope I'm NOT a newbie (at
least in using new/delete).
I asked this question because I think, that ther was a bug in C++ book, from
witch I was learning.
Example from book : ============
class cTest { public :
void* operator new(unsigned long SIZE) { return new char[SIZE]; }
void operator delete(void* THIS) {
delete THIS; // <--- !!!
}
};
main() {
cTest *ptr = new cTest;
delete ptr;
}
==================
It looks like it should be
delete[] THIS;
right ?
If I'll use in this example delete[] THIS, will then everything be allright
?
I'm asking, because I was getting compiler warning :
" `void *' is not a pointer-to-object type"
unil I used :
delete[] (char*)THIS;
> Overloading operator delete is rarely necessary.
I waned to overload it like this :
void* operator new(unsigned long SIZE)
memory_needed(SIZE);
return new char[SIZE];
}
Function memory_needed() will check size of free memory, and, if ther isn't
enought, this function will
unload some data from memory.
> "Life would be much easier if I had the source code."
That's right
Rafal
- Raw text -