I was trying to use destructors for a struct. However, the compiler reported errors that Heap block at 006651F8 modified at 00665229 past requested size of 29 gift1.exe has triggered a breakpoint.
#include "stdafx.h"
struct Node{
char *name;
int age;
Node(char *n = 0, int a = 0){
this->name = _strdup(n);
age = a;
}
~Node(){
if (this->name != 0)
delete[] this->name;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Node node1("Andy", 20);
return 0;
}
May I know where went wrong?
Does it matter if I use name
instead of this->name
?
Add-on:
Even if I change it to free(this->name);
as suggested by the answers below, the same error appears. Heap block at 00EA68D8 modified at 00EA6909 past requested size of 29 gift1.exe has triggered a breakpoint.