Re: Just how dangerous is it to delete a void* ?
On Jun 1, 5:11 pm, Mathias Gaunard <loufo...@gmail.com> wrote:
Since it's a char*, there aren't any destructors to call, so no
problem there. Also, in practice on quite a lot of systems, new and
delete use malloc() and free() underneath (you can see this by
providing replacements for these functions), so the chances are that
it will not do anything wrong.
That would be quite stupid.
Then g++ is a stupid compiler.
C++ sees the difference between node allocations and array
allocations, unlike malloc/free. It should make use of that.
It does.
Try running the following program:
#include <iostream>
void *malloc(size_t s)
{
std::cout << "ER malloc: allocating " << s << " bytes\n";
return realloc(0, s);
}
struct silly
{
~silly()
{
std::cout << "Destructing silly at " << this << std::endl;
}
};
int main()
{
silly *a;
a = new silly;
delete a;
a = new silly[4];
delete[] a;
}
I get (on RHEL 4, stock gcc 4.1.0):
ER malloc: allocating 1 bytes
Destructing silly at 0x8591008
ER malloc: allocating 8 bytes
Destructing silly at 0x859100f
Destructing silly at 0x859100e
Destructing silly at 0x859100d
Destructing silly at 0x859100c
You can even see the extra bytes set aside so that the compiler can
remember how many things to delete.
-Ed
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]