55

If I want to replicate a structure in another one (in C), what are the pro&con's of :

struct1 = struct2;

vs

memcpy(&struct1, &struct2, sizeof(mystruct_t));

Are they equivalent ? Is there a difference in performance or memory use ?

3
  • 4
    Just be careful of memory allocation inside the struct. For example if you have a struct that contains a pointer to a string and you allocate memory for the string. That memory does not get copied. the pointer to the memory gets copied, but not the memory itself. In other word this type of assignment is not a deep copy. Neither is vanilla memcpy for that mater. It can get confusing as to who owns the allocated memory.
    – Pemdas
    Commented Mar 21, 2011 at 15:22
  • I think this question is more suited to Stackoverflow. Commented Mar 21, 2011 at 16:52
  • 2
    I think this question was already well answered here: stackoverflow.com/q/4931123/176769 Commented Mar 21, 2011 at 16:54

4 Answers 4

53

The struct1=struct2; notation is not only more concise, but also shorter and leaves more optimization opportunities to the compiler. The semantic meaning of = is an assignment, while memcpy just copies memory. That's a huge difference in readability as well, although memcpy does the same in this case.

Use =.

1
  • 3
    Major problem on s = mystruct between struct s and struct * s. Both work nicely with equals, so you can't see the difference by reading. *s gives you identical, while struct s gives you a copy. When it comes time to deallocate(s), this works fine with the identical struct * s; but it leaks mystruct if you foolishly do only one dealloc with the struct s version. It is also faster to work with pointers. Just keep what you're doing in mind, and then things should be fine. YMMV.
    – DragonLord
    Commented Mar 7, 2015 at 2:55
3

I'm not sure of the performance difference, although I would guess most compilers would use memcpy under the hood.

I would prefer the assignment in most cases, it is much easier to read and is much more explicit as to what the purpose is. Imagine you changed the type of either of the structs, the compiler would guide you to what changes were needed, either giving a compiler error or by using an operator= (if one exists). Whereas the second one would blindly do the copy with the possibility of causing a subtle bug.

3

Check out this conversation about the very same topic: http://bytes.com/topic/c/answers/670947-struct-assignment

Basically, there are a lot of disagreements about the corner cases in that thread on what the struct copy would do. It's pretty clear if all the members of a struct are simple values (int, double, etc.). The confusion comes in with what happens with arrays and pointers, and padding bytes.

Everything should be pretty clear as to what happens with the memcpy, as that is a verbatim copy of every byte. This includes both absolute memory pointers, relative offsets, etc.

2

There is no inherent reason why one would be better in performance than the other. Different compilers, and versions of them, may differ, so if you really care, you profile and benchmark and use facts as a basis to decide.

A straight assignment is clearer to read.

Assignment is a teeny bit riskier to get wrong, so that you assign pointers to structs rather than the structs pointed to. If you fear this, you'll make sure your unit tests cover this. I would not care about this risk. (Similarly, memcpy is risky because you might get the struct size wrong.)

1
  • There's nothing risky about memcpy if you use sizeof and the correct struct. I suppose you could get the struct wrong and there would be no error. I think you'd probably get a segmentation fault eventually though.
    – Michael K
    Commented Mar 21, 2011 at 17:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.