tags:

views:

384

answers:

4

if you have a struct consisting of complex data items. Can you assign one instance to another.

struct Test t1;
struct Test t2;
t2=t1;

i have seen that it works for simple structures, does it work for Complex structures?. How does the compiler know how to copy data items depending on their type, eg differentiating between an int and string?

+12  A: 

Yes, assignment is supported for structs. However, there are problems:

struct S {
   char * p;
};

struct S s1, s2;
s1.p = malloc(100);
s2 = s1;

Now the pointers of both structs point to the same block of memory - the compiler does not copy the pointed to data. It is now difficult to know which struct instance owns the data. This is why C++ invented the concept of user-definable assignment operators - you can write specific code to handle this case.

anon
Um, why the downvote?
anon
I upped it because reading it made me realise the error/omission in my own answer.
Clifford
+1 for noting that there is not actually any copying going on.
Tom Duckering
Why was this flagged as spam? Has someone lost control over their mouse?
Georg Fritzsche
@gf And apparently as offensive too!
anon
+3  A: 

Yes if the structure is of the same type. Think it as a memory copy.

fabrizioM
Keep in mind that there's no deep copy, pointed to memory isn't copied.
Georg
Concurrency is also an issue here.
Tim Post
@Tim Concurrency is no more an issue than it is for assignment of the built in types, like integers and doubles - assignment is not an atomic operation for these either.
anon
@Neil Sorry, it can be, but its not a debate for this question. Types are often user defined and not built in.
Tim Post
+2  A: 

This is a simple copy, just like you would do with memcpy() (indeed, some compilers actually produce a call to memcpy() for that code). There is no "string" in C, only pointers to a bunch a chars. If your source structure contains such a pointer, then the pointer gets copied, not the chars themselves.

Thomas Pornin
+1  A: 

Did you mean "Complex" as in complex number with real and imaginary parts? This seems unlikely, so if not you'd have to give an example since "complex" means nothing specific in terms of the C language.

You will get a direct memory copy of the structure; whether that is what you want depends on the structure. For example if the structure contains a pointer, both copies will point to the same data. This may or may not be what you want; that is down to your program design.

To perform a 'smart' copy (or a 'deep' copy), you will need to implement a function to perform the copy. This can be very difficult to achieve if the structure itself contains pointers and structures that also contain pointers, and perhaps pointers to such structures (perhaps that's what you mean by "complex"), and it is hard to maintain. The simple solution is to use C++ and implement copy constructors and assignment operators for each structure or class, then each one becomes responsible for its own copy semantics, you can use assignment syntax, and it is more easily maintained.

Clifford
I edited it while reading it back, and then your comment appeared! I have suggested both assignment operator and copy constructor should be implemented.
Clifford