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?