Up until recently, I have only seen copying of structure fields done with memcpy()
. In classes and online instructions, copying the contents of one struct into
This could not be the exact answer you looking for.
Im explaining scenario which I met.
when we use memcpy()
, it does byte-by-byte copy to destination. so no worry about data alignment in ARM architecture. If you use =
operator, and any one of the address is not aligned to 4-byte then alignment fault will come.
From Arm site:
A pointer to the destination location that is one byte beyond the last byte written to. This enables continuation of the writing process with perfect alignment of bytes
for string concatenation of memory blocks.
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0175k/Cihbbjge.html
I'm resurrecting this old question because the answers do not explain why memcpy
is actually preferred.
memcpy
is preferred because it makes it clear the programmer wants to copy the content and not simply the pointers.
In the following example, the two assignments make two very different things:
struct Type *s1,*s2;
*s1=*s2;
s1=s2;
Inadvertently using one instead of the other may have disastrous effects. The compiler won't complain. Unless the program crashes when an uninitialized pointer is used, the error can go unnoticed for a long time and produce strange side effects.
Writing it as one of:
memcpy(s1,s2,sizeof(*s1));
memcpy(s1,s2,sizeof(*s2));
memcpy(s1,s2,sizeof(struct Type));
let the reader knows that the intent is to copy the content (at the expense of type safety and bounds checking).
Some compilers (gcc for instance) even issue a warning about the sizeof when they encounter something like:
memcpy(s1,s2,sizeof(s1));
People working on embedded platform will prefer to use memcopy instead of direct assignment of structure . Mainly when you deal with embedded platform, some compiler doesn't support direct structure assignment, for that you need to use memcopy. if you are working on pc then there is no issue in either case, Both are valid.
Both methods are equivalent, and perform a shallow copy. This means that the structure itself is copied, but anything the structure references is not copied.
As for why memcpy
is more popular, I'm not sure. Older versions of C did not support structure assignment (although it was a common extension as early as 1978), so perhaps the memcpy style stuck as a way of making more portable code? In any case, structure assignment is widely supported in PC compilers, and using memcpy
is more error-prone (if you get the size wrong, Bad Things are likely to happen), and so it's best to use structure assignment where possible.
There are, however, cases where only memcpy
works. For example:
memcpy
, as structure assignment requires both source and destination to be aligned properly.memcpy
, and factor this additional information into the size field.memcpy
rather than looping and copying the structures individually. Then again, it may not. It's hard to say, memcpy
implementations differ in their performance characteristics.Note also that although in C memcpy
and structure assignment are usually equivalent, in C++ memcpy
and structure assignment are not equivalent. In general C++ it's best to avoid memcpy
ing structures, as structure assignment can, and often is, overloaded to do additional things such as deep copies or reference count management.
Some people prefer memcpy because that's what they learned and they never figured out that they could just do an assignment (in ancient times the assignment wasn't allowed, but that's a long long time ago). There are no alignment problems to worry about since memory allocated by malloc () is always aligned correctly. And since a compiler could trivially translate this assignment to a memcpy call, it would never be slower or more code than memcpy. Of course there are embedded systems with badly outdated compilers.