Take a look at this program.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* link;
Node()
{
data = 0;
link = NULL;
}
};
void destroy(Node* &p)
{
Node* pre;
while(p !=NULL)
{
pre = p;
p = p->link;
delete pre; // When running for the second time, pre value becomes
// 0x602030 but when the statement executes , it doesn't
// cause a SIGABRT
}
}
int main()
{
Node* ints;
ints = new Node; // link a node
ints->link = new Node; // link another node
delete ints->link; // Delete the second node (value: 0x602030)
// If I run the above statement again in main, it will casue a SIGABRT
//signal with the following message. media/<My project location>/dist/
//debug/ cppapp: double free or corruption (fasttop): 0x0000000000602030 ***
destroy(ints);
return 0;
}
I'm using debug mode and if I run "delete ints->link" statement twice in main, gdb will say the program cause a SIGABRT, when it hits the second delete statement because I free the same memory twice. But this doesn't happen when I run destroy method like in the above code.
In other words, According to my knowledge I'm freeing the same memory twice in the above program. It should cause a SIGABRT signal, right? but it doesn't. I need to know whether it's my mistake(ie. understanding in a wrong way or code error) or something wrong with the compiler?
I'm using g++ 4.4.3, Netbeans IDE, Ubuntu 10.4, gdb. I used gdb to see where the pointers point.