复制结构下与分配,而不是的memcpy() [英] Copying structure in C with assignment instead of memcpy()

查看:420
本文介绍了复制结构下与分配,而不是的memcpy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,我只看到与进行结构域的memcpy的复制()。在课程和在线指导,复制一幅结构的内容到另一个通常看起来像

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 another generally looks like

struct block *b0 = malloc(sizeof(struct block));
struct block *b1 = malloc(sizeof(struct block));
/* populate fields in *b0 */
memcpy(b1, b0, sizeof *b1); /* copy contents of b0 into b1 */
/* free b0, b1 */

然而,这个任务也可以通过简单的赋值替换的memcpy()来完成

*b1 = *b0; /* dereferenced struct assignment */

是否有很好的理由,这不是作为广泛使用的(在我有限的经验至少)?在这两种方法,分配和的memcpy()当量的其他,或者是有一些令人信服的理由使用的memcpy()一般?

Is there good reason why this isn't as widely used (at least in my limited experience)? Are these two methods—assignment and memcpy()—equivalent, or is there some compelling reason to use memcpy() in general?

推荐答案

这两种方法是等效的,并执行的浅拷贝的。这意味着该结构本身被拷贝,但任何结构的引用不被复制。

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.

至于为什么的memcpy 是比较受欢迎的,我不知道。的C旧版本不支持结构分配(<一个href=\"http://www.velocityreviews.com/forums/t443010-struct-assignment-historical-question.html\">although这是一个常见的​​扩展,早在1978年),所以也许memcpy的风格坚持为使更多的便携式code的一种方式?在任何情况下,结构分配广泛支持PC编译器,并使用的memcpy 更容易出错(如果你的尺寸错了,坏的东西都是可能发生),所以,最好尽可能使用结构分配。

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.

有,但是,当仅的memcpy 的作品。例如:

There are, however, cases where only memcpy works. For example:


  • 如果您正在复制一个结构或不对齐的缓冲 - 例如,保存/负载/从磁盘或发送/接收网络上的 - 你需要使用的memcpy 的,如结构分配既需要源和目标正确对齐。

  • 如果你的结构后打包更多的信息,可能使用零元素的数组,你需要使用的memcpy 和因子此附加信息到大小字段。

  • 如果您正在复制结构数组,它的可以的是更有效地做一个的memcpy ,而不是循环和复制结构个别。话又说回来,也可能不是。这很难说,的memcpy 实现他们的性能特点不同。

  • 有些嵌入式编译器可能不支持结构分配。还有可能是其他问题的编译器不支持更重要的事情为好,当然。

  • 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.

还要注意的是,虽然用C 的memcpy 和结构的分配通常是等价的,在C ++ 的memcpy 和结构分配的的的等价物。一般来说C ++中,最好避免的memcpy ING结构,因为结构的分配就可以了,而且经常是,超载做更多的事情,如深拷贝或引用计数管理。

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.

这篇关于复制结构下与分配,而不是的memcpy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆