memcpy 后堆栈损坏
我想复制一个对象并使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
dataholder
变量是一个指向backditup
大小的数组的指针,而不是数组本身。因此,当您执行Zeromemory
和memcpy
调用时,您不应该获取其地址;相反,写:不带
&
。同样,当您将数据复制回来时,您需要:最后,您需要在第二个
Zeromemory
调用中进行相同的修复 - 不过,由于您在该调用后立即删除数组,打这个电话确实没有任何意义。由于同样的原因,第二个
Zeromemory
调用中的大小也是错误的;sizeof(dataholder)
是指针的大小,而不是它指向的数组的大小。如果您不简单地完全删除此调用,则应该在此处使用 sizeof(backditup) 以便与声明保持一致,或者更好的是,声明一个变量来保存数据持有者数组的长度并使用始终如一。 (或者您可以使用数据类型的大小sizeof(testclass)
——这可能是最好的选择。)最后,正如 Mark Wilkins 在他的回答中指出的那样,您需要使用
删除数组>delete[]
,而不是delete
,以避免损坏堆。Your
dataholder
variable is a pointer to an array the size ofbackditup
, not the array itself. Thus, when you do theZeromemory
andmemcpy
calls, you should not take its address; instead, write:without the
&
. Likewise, when you copy the data back, you want: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 usesizeof(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[]
, notdelete
, to avoid corrupting the heap.我不确定这是否是问题的一部分,但删除应该是:
更重要的是,ZeroMemory 调用不应该传递 dataholder 的地址(& dataholder)而是它的值(它指向什么):
I'm unsure if this would be part of the problem, but the
delete
should be:More importantly, the ZeroMemory call should not be passing the address of
dataholder
(&dataholder) but rather its value (what it points to):您正在覆盖 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.