memcpy 后堆栈损坏

发布于 2025-01-02 08:38:16 字数 1041 浏览 2 评论 0原文

我想复制一个对象并使用winsock通过网络发送它,但是有一个问题。如果我将对象复制到堆上的字符数组,则会破坏堆栈。这是我的代码:

testclass backditup;       //This is an object with information
testclass restorenaarhier; //I will copy it to this object

backditup.setvalues(100,100);
restorenaarhier.setvalues(0,0);

char * dataholder = new char[sizeof(backditup)];  //This is the data holder
ZeroMemory(&dataholder,sizeof(backditup));

memcpy(&dataholder,&backditup,sizeof(backditup)); //Save it to the char[]

//Sending it over the network
//resieving the object

//Store the data on the same object 
memcpy(&restorenaarhier,&dataholder,sizeof(restorenaarhier));

//deleting the data holder
ZeroMemory(&dataholder,sizeof(dataholder));
delete dataholder;

//output the code
restorenaarhier.echo();

代码将正常工作,但是当我在调试模式下编译它时,我得到的结果是:
http://imageshack.us/photo/my-images/839/errormnr。 png/

运行时检查失败 #2 变量“dataholder”周围的堆栈已损坏。

有人可以帮我解决这个问题吗?

I want to copy an object and send it over the network with winsock, but there is one problem. I destroy the stack if I copy an object to an array of chars on the heap. Here is my code:

testclass backditup;       //This is an object with information
testclass restorenaarhier; //I will copy it to this object

backditup.setvalues(100,100);
restorenaarhier.setvalues(0,0);

char * dataholder = new char[sizeof(backditup)];  //This is the data holder
ZeroMemory(&dataholder,sizeof(backditup));

memcpy(&dataholder,&backditup,sizeof(backditup)); //Save it to the char[]

//Sending it over the network
//resieving the object

//Store the data on the same object 
memcpy(&restorenaarhier,&dataholder,sizeof(restorenaarhier));

//deleting the data holder
ZeroMemory(&dataholder,sizeof(dataholder));
delete dataholder;

//output the code
restorenaarhier.echo();

The code will work properly, but when I compile this in debug mode I get at the end:
http://imageshack.us/photo/my-images/839/errormnr.png/

Run-Time Check Failure #2 Stack around the variable 'dataholder' was corrupted.

Can someone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

奈何桥上唱咆哮 2025-01-09 08:38:16

您的 dataholder 变量是一个指向 backditup 大小的数组的指针,而不是数组本身。因此,当您执行 Zeromemorymemcpy 调用时,您不应该获取其地址;相反,写:

ZeroMemory(dataholder,sizeof(backditup));
memcpy(dataholder,&backditup,sizeof(backditup));

不带 &。同样,当您将数据复制回来时,您需要:

memcpy(&restorenaarhier,dataholder,sizeof(restorenaarhier));

最后,您需要在第二个 Zeromemory 调用中进行相同的修复 - 不过,由于您在该调用后立即删除数组,打这个电话确实没有任何意义。

由于同样的原因,第二个 Zeromemory 调用中的大小也是错误的; sizeof(dataholder) 是指针的大小,而不是它指向的数组的大小。如果您不简单地完全删除此调用,则应该在此处使用 sizeof(backditup) 以便与声明保持一致,或者更好的是,声明一个变量来保存数据持有者数组的长度并使用始终如一。 (或者您可以使用数据类型的大小 sizeof(testclass) ——这可能是最好的选择。)

最后,正如 Mark Wilkins 在他的回答中指出的那样,您需要使用 删除数组>delete[],而不是delete,以避免损坏堆。

Your dataholder variable is a pointer to an array the size of backditup, not the array itself. Thus, when you do the Zeromemory and memcpy calls, you should not take its address; instead, write:

ZeroMemory(dataholder,sizeof(backditup));
memcpy(dataholder,&backditup,sizeof(backditup));

without the &. Likewise, when you copy the data back, you want:

memcpy(&restorenaarhier,dataholder,sizeof(restorenaarhier));

And, finally, you need to make the same fix in the second Zeromemory call -- although, since you are deleting the array immediately after that call, there really is no point in having that call at all.

The size in the second Zeromemory call is erroneous for the same reason; sizeof(dataholder) is the size of the pointer, not the array it's pointing to. If you don't simply delete this call entirely, you should either use sizeof(backditup) here for consistency with the declaration, or better yet, declare a variable to hold the length of the dataholder array and use that consistently. (Or you can use the size of the data type, sizeof(testclass) -- that's probably the best option.)

Finally, as Mark Wilkins noted in his answer, you need to delete arrays with delete[], not delete, to avoid corrupting the heap.

羁拥 2025-01-09 08:38:16

我不确定这是否是问题的一部分,但删除应该是:

delete[] dataholder;

更重要的是,ZeroMemory 调用不应该传递 dataholder 的地址(& dataholder)而是它的值(它指向什么):

ZeroMemory(dataholder ...

I'm unsure if this would be part of the problem, but the delete should be:

delete[] dataholder;

More importantly, the ZeroMemory call should not be passing the address of dataholder (&dataholder) but rather its value (what it points to):

ZeroMemory(dataholder ...
守不住的情 2025-01-09 08:38:16

您正在覆盖 memcpy 调用中的堆栈。原因是您正在获取保存缓冲区地址的变量的地址。您想要的只是缓冲区的地址。

在 Zeromemory 和 memcpy 调用中使用“dataholder”而不是“&dataholder”。

You are overwriting the stack in your memcpy calls. The reason is that you are taking the address of the variable which hold the address of your buffer. All you want is the address of your buffer.

Use "dataholder" not "&dataholder" in the Zeromemory and memcpy calls.

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