结构体赋值还是memcpy?
如果我想在另一个结构中复制一个结构(在 C 语言中),那么以下结构的优点和缺点是什么:
struct1 = struct2;
vs
memcpy(&struct1, &struct2, sizeof(mystruct_t));
它们是否等效?性能或内存使用有差异吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
struct1=struct2;
表示法不仅更简洁,而且更短,为编译器留下了更多优化机会。=
的语义是赋值,而memcpy
只是复制内存。尽管memcpy
在这种情况下做了同样的事情,但可读性方面也存在巨大差异。使用
=
。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, whilememcpy
just copies memory. That's a huge difference in readability as well, althoughmemcpy
does the same in this case.Use
=
.我不确定性能差异,尽管我猜大多数编译器都会在幕后使用 memcpy。
在大多数情况下,我更喜欢作业,它更容易阅读,并且对于目的是什么更加明确。想象一下,您更改了任一结构的类型,编译器将指导您需要进行哪些更改,要么给出编译器错误,要么使用运算符=(如果存在)。而第二个会盲目地进行复制,有可能导致微妙的错误。
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.
查看有关同一主题的对话:http://bytes.com/topic /c/answers/670947-struct-assignment
基本上,对于该线程中关于结构副本将做什么的极端情况存在很多分歧。结构体的所有成员是否都是简单值(int、double 等)是非常清楚的。混乱来自于数组和指针以及填充字节的情况。
关于
memcpy
发生的一切都应该非常清楚,因为它是每个字节的逐字副本。这包括绝对内存指针、相对偏移量等。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.没有任何内在原因可以解释为什么一个人的表现会比另一个人更好。不同的编译器及其版本可能会有所不同,因此如果您确实关心,您可以进行分析和基准测试,并使用事实作为决定的基础。
直接的作业读起来更清晰。
分配出错的风险有点大,因此您将指针分配给结构而不是指向的结构。如果您担心这一点,您将确保您的单元测试涵盖了这一点。我不会关心这个风险。 (同样,memcpy 也有风险,因为您可能会得到错误的结构大小。)
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.)