This may be a very basic question but somehow it got me tricked... when I write test code, it seems to work, but something is going wrong in production.
// Header file
#define length 100
typedef struct testStr_t {
int a;
char b;
char t1[length];
char t2[length];
} test;
void populateTest(test*);
// source file
test test1;
test test2;
populateTest(&test1);
test2 = test1;
Will test2
be a deep copy of test1
? Or are there gotchas here? Does it matter if the code is compiled with a C compiler or a C++ compiler?
Deep copies are only hindered by pointers, so your struct
will be copied correctly in C. It'll work in C++ as well unless you define your own operator=
that doesn't copy correctly. You only need to define operator=
for types with pointers, since a shallow copy of a pointer will copy the pointer but share the data.