"onsbomma" <on******@hotmail.com> wrote in message
news:10***************@seven.kulnet.kuleuven.ac.be ...
Hello
I know this is not good code, but i was wondering why dubble delete
gives an error (not an exception!!) and dubble free doesn't??
What sort of error? I don't think the code below should cause a compile
error.
If new/delete works like malloc/free. Is the error caused by the
destructor?? and if so why???
new/delete don't work exactly like malloc/free and the destructor is one
difference. It is possible that the destructor is causing the error
(whatever that is) but it would really depend on how the destructor was
written.
Integer *m = (Integer*)malloc(sizeof(*m));
free(m);
free(m);
Integer *m = new Integer();
delete m;
delete m;
Both if these pieces of code invoke what is known as undefined behaviour. It
is a concept that some people have trouble getting to grips with, but
basically it says that if you do something that is against the rules of C++
(in this case freeing/deleting the same memory twice) then the behaviour of
your code is completely undefined. It might crash, it might output an error,
it might throw an exception, it might format you hard drive, etc. etc.
Anything might happen, INCLUDING it might work.
john