C语言 结构赋值还是memcpy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5383318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:07:22  来源:igfitidea点击:

Struct assignment or memcpy?

cstructvariable-assignmentmemcpy

提问by ofaurax

If I want to replicate a structure in another one (in C), what are the pro&con's of :

如果我想在另一个结构中复制一个结构(在 C 中),以下的优点和缺点是什么:

struct1 = struct2;

vs

对比

memcpy(&struct1, &struct2, sizeof(mystruct_t));

Are they equivalent ? Is there a difference in performance or memory use ?

它们是等价的吗?性能或内存使用有区别吗?

采纳答案by Alexander Gessler

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, while memcpyjust copies memory. That's a huge difference in readability as well, although memcpydoes the same in this case.

这种struct1=struct2;表示法不仅更简洁,而且更短,给编译器留下了更多的优化机会。的语义=是赋值,而memcpy只是复制内存。这也是可读性的巨大差异,尽管memcpy在这种情况下也是如此。

Use =.

使用=.

回答by Berin Loritsch

Check out this conversation about the very same topic: http://bytes.com/topic/c/answers/670947-struct-assignment

查看关于相同主题的对话: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.

基本上,在该线程中关于结构副本将做什么的极端情况存在很多分歧。如果结构体的所有成员都是简单值(int、double 等),这一点就很清楚了。混淆来自数组和指针以及填充字节发生的事情。

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,因为它是每个字节的逐字副本。这包括绝对内存指针、相对偏移量等。

回答by Andy Lowry

I'm not sure of the performance difference, although I would guess most compilers would use memcpy under the hood.

我不确定性能差异,尽管我猜大多数编译器会在幕后使用 memcpy。

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.

在大多数情况下,我更喜欢分配,它更容易阅读,并且更明确地说明目的是什么。想象一下,您更改了任一结构的类型,编译器将指导您进行哪些更改,要么给出编译器错误,要么使用 operator=(如果存在)。而第二个会盲目地进行复制,可能会导致一个微妙的错误。

回答by Andy Lowry

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, memcpyis risky because you might get the struct size wrong.)

赋值出错的风险很小,因此您将指针分配给结构而不是指向的结构。如果您担心这一点,您将确保您的单元测试涵盖了这一点。我不会在意这种风险。(同样,memcpy也有风险,因为您可能会弄错结构体大小。)