复制构造函数演示(崩溃...案例 2)
请看一下这个程序:
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;
}
这个程序在执行时崩溃了。错误:“表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)”
如果我使用分配运算符将“Hai”分配给名称,则会崩溃。当我使用 string func _tcscpy 为名称分配“Hai”时,它工作得很好。有人可以解释为什么会这样吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不会将“Hai”的内容复制到
name
中,而是name
将指向只读存储器(其内容是“Hai”)如果您稍后尝试删除名称,那么它可能会崩溃。
You are not copying contents of "Hai" into
name
insteadname
will point to a read only memory ( whose contents are "Hai")if you try to delete name later then it might crash.
在默认构造函数中,
您将字符串文字的地址分配给指针,并在析构函数中对其调用
delete[]
,这是未定义的行为。delete[]
只能在new[]
返回的地址上调用。当您使用
_tcscpy()
时,您将文字内容复制到由new[]
分配的缓冲区,然后析构函数运行正常。In the default constructor
you assign the address of a string literal to the pointer and in the destructor you call
delete[]
on it, that's undefined behavior.delete[]
should only be called on addresses returned bynew[]
.When you instead use
_tcscpy()
you copy the literal content to the buffer allocated bynew[]
and then the destructor runs fine.当您使用赋值时,您使指针
name
指向字符串文字“Hai”。这稍后会在析构函数中被删除。但是,字符串文字没有用new
分配,并且不能像这样删除,因此您会得到未定义的行为。您只能通过delete
来取消分配您通过new
分配的内容。这与复制构造函数无关。When you use assignment, you make the pointer
name
point at the string literal "Hai". This later gets deleted in the destructor. However, the string literal was not allocated withnew
, and cannot be deleted like this, so you get undefined behaviour. You can only deallocate withdelete
things you allocated withnew
. This has nothing to do with the copy constructor.这里,您没有将数据复制到
new
分配的内存中。相反,您将一个新值分配给指向只读位置的指针name
(在大多数情况下)。由于该内存不是使用new
分配的,因此您无法对其进行delete
操作。另请注意,这里存在内存泄漏,因为使用new char[20];
分配的内存永远不会被删除。Here you are not copying the data into the memory allocated by
new
. Instead you are assigning a new value to pointername
which points at read-only location (in most cases). Since this memory was not allocated usingnew
you can not dodelete
on it. Also, note that you have a memory leak here as the memory allocated usingnew char[20];
is never deleted.完全相同的程序,但是用 C++ 编写:
我的程序可以工作,很清楚,而且我输入的内容比你少;)
The very same program, but in C++:
Mine works, is clear, and I typed less than you did ;)
您在该代码中所做的就是为名称分配一个内存块(为名称指针分配一个地址)。然后,您实际上用字符串文字“Hai”的地址(在构造函数完成后不再存在)覆盖该地址。这就是您收到错误的原因,因为析构函数尝试释放不属于您的内存。 (你没有分配它)。
What you do in that code is that you allocate a memory block for the name (assign an address to name pointer). Then you actually overwrite this address by the address of the string literal "Hai" (which ceases to exist after the constructor finishes). Thats why you get the error, since the destructor tries to free memory which does not belong to you. ( You did not allocate it ).