Copy constructor demo (crashing... case 2)
- by AKN
Please have a glance at this program:
class CopyCon
{
public:
char *name;
CopyCon()
{
name = new char[20];
name = "Hai";//_tcscpy(name,"Hai");
}
CopyCon(const CopyCon &objCopyCon)
{
name = new char[_tcslen(objCopyCon.name)+1];
_tcscpy(name,objCopyCon.name);
}
~CopyCon()
{
if( name != NULL )
{
delete[] name;
name = NULL;
}
}
};
int main()
{
CopyCon obj1;
CopyCon obj2(obj1);
cout<<obj1.name<<endl;
cout<<obj2.name<<endl;
}
This program crashes on execution. Error: "Expression: _BLOCK_TYPE_IS_VALID(pHead-nBlockUse)"
If I assign "Hai" to name using aasignment operator, its crashing. Where as when I use string func _tcscpy to assign "Hai" to name, its working perfectly. Can some one explain why so?