使用赋值而不是 memcpy() 在 C 中复制结构 [英] Copying structure in C with assignment instead of memcpy()

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

问题描述

直到最近,我才看到使用 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()来完成.

However, this task can also be accomplished by a simple assignment replacing the 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 不支持结构赋值(尽管它早在 1978 年就已经是一个常见的扩展),所以也许 memcpy 风格作为一种制作更可移植代码的方式?在任何情况下,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 结构,因为结构赋值可以而且经常被重载以执行其他操作,例如深度复制或引用计数管理.

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() 在 C 中复制结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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