Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made the following code to copy struct and classes contents. The result is ok in VS C++ but i do not know if it is right in linux.
There is programmers that said about "shallow copy" and need of use of strdup, see here the 4th response of "paxdiablo":
http://stackoverflow.com/questions/4931123/copying-one-structure-to-another

but I tested this using VS2013 and not saw any "shadow copy":

What I have tried:

#include <iostream>


struct s_struct
{
	int a, b;
	double w;
}s1,s2,s3,s4;

class c_class
{
public:
	int a, b;
	double w;
}c1,c2,c3;


using namespace std;


void main()
{
	s1.a = 10; s1.b = 12; s1.w = 100.111;
	memcpy(&s2, &s1, sizeof(s_struct));
	s3 = s1;
	if ((&s1 == &s3) || (&s1==&s2))
		cout << "STUPID copy of structures" << endl;
	else
		cout << "right copy of structures" << endl;
	c1.a = 22; c1.b = 24; c1.w = 200.222;
	memcpy(&c2, &c1, sizeof(c_class));
	c3 = c1;
	if ((&c1 == &c3) || (&c1 == &c2))
		cout << "STUPID copy of classes" << endl;
	else
		cout << "right copy of classes" << endl;


	cout << "=================== END ======================" << endl << endl;
	getchar(); getchar();
}
Posted
Updated 6-Nov-16 22:27pm

That is OK as far as it goes, but it would not work if either the struct or the class contained pointers to other objects. You should create proper copy constructors as described at Copy Constructors and Copy Assignment Operators (C++)[^].

Note also this has nothing to do with Windows or Linux, it is purely an issue for C++.
 
Share this answer
 
It is OK to just copy the memory when your class or struct contains only fundamental members (is_fundamental - C++ Reference[^]) as in your example.

Note that there is usually an implicitly-declared copy assignment operator which handles also class members (calls their copy operator). See Copy assignment operator - cppreference.com[^].

If your class allocates memory storing the pointer in a member variable, you should always provide a copy operator (as in the mentioned SO link where strdup is used to copy the content of an allocated string).

There is no difference for Windows and Linux. It's just C++.
 
Share this answer
 
Comments
Javier Luis Lopez 6-Jul-17 7:19am    
The copy operator is a way.

Copying the struct or class to a character array also would be useful to send it through tcpip or save it at a file in this way:

char *buffer=new char[sizeof(class_tobe_copied)];
memcpy(buffer,&class_tobe_copied,sizeof(class_tobe_copied));

//...transmit the data
//Recover the class to class2:
memcpy(&class2,buffer,sizeof(class2));


I have a doubth:
Is this method safe using different OS and compilers at the encode and decode side?
Jochen Arndt 6-Jul-17 7:34am    
It is usually not safe because the sizeof() operator may return different sizes. To ensure identical sizes you have to use identical packing methods (see #pragma pack for VC and __attribute__((packed)) for GCC).

Your example will also fail if it contains non fundamental members.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900