C++嵌入许多简单变量和一个嵌套结构的结构的深度复制..(memcpy?)

发布于 2024-11-01 15:00:52 字数 607 浏览 1 评论 0原文

我正在

struct  Parent
{
    int child1;
    int child2;
     char child3;
     float child 4;
     anotherStruct child5;
};

typedef struct
{
    unsigned char  x;
    int            y;
    char           z;
    float          a;
    int            b;
    char           c;
    etc ..
  } anotherStruct;

Parent   myFirstParent;
Parent   mySecondParent;

///I want to do a deep copy of myFirstParent into mySecondParent. 
//does the follwowing work for that purpose??
memcpy (&mySecondParent, &myFirstParent, sizeof(myFirstParent);

自己调查答案,但同时由于时间限制,我发布了问题。提前致谢。

I have

struct  Parent
{
    int child1;
    int child2;
     char child3;
     float child 4;
     anotherStruct child5;
};

typedef struct
{
    unsigned char  x;
    int            y;
    char           z;
    float          a;
    int            b;
    char           c;
    etc ..
  } anotherStruct;

Parent   myFirstParent;
Parent   mySecondParent;

///I want to do a deep copy of myFirstParent into mySecondParent. 
//does the follwowing work for that purpose??
memcpy (&mySecondParent, &myFirstParent, sizeof(myFirstParent);

I am investigating the answer myself but meanwhile I am posting the question because of extreme time constraint. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤单情人 2024-11-08 15:00:52

在您的情况下,赋值应该可以正常工作:

mySecondParent = myFirstParent;

毕竟,没有一个结构定义指针。

另外,如果有指针,即使 memcpy 也无法工作,因为它不进行深度复制。在这种情况下,您必须手动复制指针的内容

顺便说一句,您的代码无法编译,因为您在定义它之前使用了 anotherStruct 。请在 Parent 之前定义 anotherStruct

In your case, assignment should work just fine:

mySecondParent = myFirstParent;

After all, none of the structs define pointers.

Also, if there were pointers, even memcpy would not work, as it doesn't do deep-copy. In that case, you had to manually copy the content of the pointers.

By the way, your code wouldn't compile, as you're using anotherStruct before defining it. Please define anotherStruct before Parent.

顾挽 2024-11-08 15:00:52

根据经验

请记住一件事:memcpy() 是邪恶的!这是一个 C 结构,你不应该在 C++ 中使用它。 memcpy() 对位和字节进行操作,而不是对对象进行操作。最坏的情况是,它无法考虑正在复制的对象中实际存储的内容,从而导致错误的结果,甚至导致考虑特殊情况的更丑陋的代码。切勿在 C++ 代码中使用 memcpy()。总是有更好、更面向对象的方法来完成同样的事情。

这可能不适用于您的情况,因为您没有使用免费商店,但适用于您可能使用免费商店的情况。

来源

As a rule of thumb

Keep in mind one thing: memcpy() is evil! It's a C construct you should never use in C++. memcpy() operates on bits and bytes, not on objects. At worst, it fails to take into account what's actually being stored in the objects you're copying, leading to erroneous results, or even uglier code that takes the special cases into account. Never use memcpy() in C++ code. There are always better, more object-oriented ways to do the same thing.

This mayn't apply to your case as you aren't using the free store, but for times when you may use the free store.

SOURCE

冰雪梦之恋 2024-11-08 15:00:52

由于您的类中没有指针,因此默认编译器生成的复制构造函数和 allocateemnt 运算符版本将正常工作(执行您所说的深层复制)。

从技术上讲,它是一个浅拷贝。深层复制是指您的类包含指针并且您需要复制指针内容。

这应该可以在没有 memcpy 的情况下正常工作(永远不应该使用 memcpy 来复制 C++ 对象)

Parent   p1;
Parent   p2(p1);   // Make a copy of p1 into p2.
Parent   p3;

p1 = p3; // Use assignment operator works fine.

As there are no pointers in your class then the default compiler generated versions of the copy constructor and assignemnt operator will work just fine (doing what you call a deep copy).

Technically it is a shallow copy. A deep copy is when your class contains pointers and you need to make copies of the pointers content.

This should work fine without memcpy (which should never be used to copy C++ objects)

Parent   p1;
Parent   p2(p1);   // Make a copy of p1 into p2.
Parent   p3;

p1 = p3; // Use assignment operator works fine.
指尖凝香 2024-11-08 15:00:52

一个简单的分配结构就可以很好地适合您。一般来说,cipiler(如 Microsoft 编译器和 GCC)会选择最有效的方法来复制内存。对于小或简单的事情,一系列寄存器加载和保存通常是性能最好的。编译器可能会选择 memcpy() 来处理某些事情。

其他答案表明 memcpy() 永远不应该与 C++ 类一起使用。这通常是正确的,但如果您知道自己在做什么,则可以在某些情况下使用它。但这几乎是不必要的。

在 AMD 和 Intel x86/x64 处理器上,memcpy() 始终生成(或使用)“rep movingd”指令。多年来,所有现代处理器都针对此指令进行了优化,具有特殊的微代码或其他内部选项,以确保中小型内存移动的良好性能。

最好的办法就是在调试器中查看生成的汇编语言代码。如果您的结构分配最终不是一系列简单的加载/存储或 memcopy (rep movingd),那么您可以自己使用 memcpy() 编写一个。由于您的类和结构正在使用“纯旧数据”,因此这会很好地工作。正如另一个答案所说,如果您的结构包含指针,那么您将需要代码来将它们处理为浅拷贝或深拷贝,或引用计数对象。

与往常一样,对于“热”代码路径,分析器和性能测量是必不可少的工具。

A simple assigment strcture will work fine for you. In general, the cimpiler (like the Microsoft compiler, and likey GCC) will pick the most efficient method for copying memory. For small or simple things, a series of register loads and saves are often the most well performing. The compiler may choose memcpy() for some things.

Other answers indicate that memcpy() should never be used wiht C++ classes. This is generaly correct, but it can be used in some cases if you know whatyou are doing. But this is almost never ncessary.

On AMD and Intel x86/x64 processors, memcpy() is always generated (or uses) the 'rep movesd' instruction. For many, many years, all modern processors are optimized for thi sinstruction, having special micro-code or other internal optmitions to ensure well performing small and mediaum sized memory moves.

THe best thing to do is simply look at the generated assembly language code in your debugger. If your structure assignment does not end up with a series of simple load/stores, or memcopy (rep movesd) then you you can code one using memcpy() yourself. This will work fine since your classes and strctures are using "plain-old-data". As anohter answer says, if your structure includes pointers then you will need code to handle those as shallow or deep copies, or reference counted objects.

As always, for 'hot' code paths, the profiler and performance measurment are essential tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文