复制构造函数演示(崩溃...案例 2)

发布于 2024-09-04 22:20:39 字数 694 浏览 11 评论 0原文

请看一下这个程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓦然回首 2024-09-11 22:20:39
 name = "Hai";//_tcscpy(name,"Hai");

您不会将“Hai”的内容复制到name中,而是name将指向只读存储器(其内容是“Hai”)
如果您稍后尝试删除名称,那么它可能会崩溃。

 name = "Hai";//_tcscpy(name,"Hai");

You are not copying contents of "Hai" into name instead name will point to a read only memory ( whose contents are "Hai")
if you try to delete name later then it might crash.

眼眸里的快感 2024-09-11 22:20:39

在默认构造函数中,

CopyCon()
{ 
    name = new char[20];        
    name = "Hai";//_tcscpy(name,"Hai");
}

您将字符串文字的地址分配给指针,并在析构函数中对其调用 delete[] ,这是未定义的行为。 delete[] 只能在 new[] 返回的地址上调用。

当您使用 _tcscpy() 时,您将文字内容复制到由 new[] 分配的缓冲区,然后析构函数运行正常。

In the default constructor

CopyCon()
{ 
    name = new char[20];        
    name = "Hai";//_tcscpy(name,"Hai");
}

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 by new[].

When you instead use _tcscpy() you copy the literal content to the buffer allocated by new[] and then the destructor runs fine.

哎呦我呸! 2024-09-11 22:20:39

当您使用赋值时,您使指针 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 with new, and cannot be deleted like this, so you get undefined behaviour. You can only deallocate with delete things you allocated with new. This has nothing to do with the copy constructor.

最美的太阳 2024-09-11 22:20:39
name = new char[20];        
name = "Hai";//_tcscpy(name,"Hai");

这里,您没有将数据复制到new分配的内存中。相反,您将一个新值分配给指向只读位置的指针name(在大多数情况下)。由于该内存不是使用 new 分配的,因此您无法对其进行 delete 操作。另请注意,这里存在内存泄漏,因为使用 new char[20]; 分配的内存永远不会被删除。

name = new char[20];        
name = "Hai";//_tcscpy(name,"Hai");

Here you are not copying the data into the memory allocated by new. Instead you are assigning a new value to pointer name which points at read-only location (in most cases). Since this memory was not allocated using new you can not do delete on it. Also, note that you have a memory leak here as the memory allocated using new char[20]; is never deleted.

谁把谁当真 2024-09-11 22:20:39

完全相同的程序,但是用 C++ 编写:

struct CopyCon
{
  CopyCon(): name("HAI") {}
  std::string name;
};

int main(int argc, char* argv[])
{
  CopyCon obj1;
  CopyCon obj2(obj1);
  cout<<obj1.name<<endl;
  cout<<obj2.name<<endl;
}

我的程序可以工作,很清楚,而且我输入的内容比你少;)

The very same program, but in C++:

struct CopyCon
{
  CopyCon(): name("HAI") {}
  std::string name;
};

int main(int argc, char* argv[])
{
  CopyCon obj1;
  CopyCon obj2(obj1);
  cout<<obj1.name<<endl;
  cout<<obj2.name<<endl;
}

Mine works, is clear, and I typed less than you did ;)

梦里°也失望 2024-09-11 22:20:39

您在该代码中所做的就是为名称分配一个内存块(为名称指针分配一个地址)。然后,您实际上用字符串文字“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 ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文