Copying structure in C with assignment instead of memcpy()

后端 未结 5 1023
孤街浪徒
孤街浪徒 2020-12-08 15:07

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

相关标签:
5条回答
  • 2020-12-08 15:36

    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

    0 讨论(0)
  • 2020-12-08 15:38

    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));
    
    0 讨论(0)
  • 2020-12-08 15:42

    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.

    0 讨论(0)
  • 2020-12-08 15:44

    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:

    • If you're copying a structure to or from an unaligned buffer - eg, to save/load to/from disk or send/receive on a network - you need to use memcpy, as structure assignment requires both source and destination to be aligned properly.
    • If you're packing additional information after a structure, perhaps using a zero-element array, you need to use memcpy, and factor this additional information into the size field.
    • If you're copying an array of structures, it may be more efficient to do a single 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.
    • Some embedded compilers might not support structure assignment. There's probably other more important things the compiler in question doesn't support as well, of course.

    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 memcpying structures, as structure assignment can, and often is, overloaded to do additional things such as deep copies or reference count management.

    0 讨论(0)
  • 2020-12-08 15:53

    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.

    0 讨论(0)
提交回复
热议问题