简体   繁体   English

如何复制包含非原始类型的数组?

[英]How do I copy arrays that contain non primitive types?

I'm writing C++ on an Arduino. 我在Arduino上编写C ++。 I've run into a problem trying to copy and array using memcpy . 我在尝试使用memcpy复制和数组时遇到问题。

Character characters[5] = {
    Character("Bob", 40, 20),
    Character("Joe", 30, 10),
    ...
};

I then pass this array into a constructor like so: 然后,我将此数组传递给构造函数,如下所示:

Scene scene = Scene(characters, sizeof(characters)/sizeof(Character));

Inside this constructor I attempt to copy the characters using memcpy: 在此构造函数中,我尝试使用memcpy复制字符:

memcpy(this->characters, characters, characters_sz);

This seems to lock up my application. 这似乎锁定了我的应用程序。 Upon research it appears that memcpy is not the right tool for this job. 经过研究,似乎memcpy不是适合此工作的工具。 If I comment that line out the rest of the application continues to freeze. 如果我对此发表评论,那么该应用程序的其余部分将继续冻结。

I can't use vectors because they're not supported on the Arduino, neither is std::copy . 我不能使用矢量,因为Arduino不支持矢量, std::copy也不支持。 Debugging is a pain. 调试很痛苦。

Is there any way to do this? 有什么办法吗?

Edit 编辑

The reason why I am copying is because multiple objects will get their own copy of the characters. 我复制的原因是因为多个对象将获得他们自己的字符副本。 Each class can modify and destroy them accordingly because their copies. 每个类都可以相应地修改和销毁它们,因为它们是副本。 I don't want to have the Scene class responsible for creating the characters, so I'd rather pass them in. 我不想让Scene类负责创建角色,所以我宁愿将它们传递给我。

您将必须分别复制成员,或在Character类/结构中创建复制构造函数

It's very unclear what's going on in your code. 尚不清楚代码中正在发生什么。

  • First of all, you aren't using std::array as your question title suggests, you are using a built-in array. 首先,您没有使用问题标题所建议的std::array ,而是使用了内置数组。 You could concievably use std::array instead, and just use copy constructor of std::array . 您可以方便地使用std::array ,而只需使用std::array副本构造函数。 But that brings us to second question. 但这使我们想到了第二个问题。

  • When you are doing memcpy in the constructor of Scene , what is the actual size of this->characters ? 当您在Scene的构造函数中执行memcpy时, this->characters的实际大小是多少? It's not a good thing to have a constructor that takes characters_sz dynamically if in fact there is a static limit on how many it can accept. 如果实际上可以容纳多少个静态限制,那么拥有一个动态获取characters_sz的构造函数并不是一件好事。

If I were you and really trying to avoid dynamic allocations and std::vector , I would use std::array for both things, the member of Scene and the temporary variable you are passing, and I would make the constructor a template, so that it can accept arbitrary sized std::array of characters. 如果我是您,并且确实在尝试避免动态分配和std::vector ,那么我将对所有两个对象( Scene成员和您要传递的临时变量)使用std::array ,并且将构造函数设为模板,因此它可以接受任意大小的std::array字符。 But, I would put a static assert so that if the size of the passed array is too large, it fails at compile time. 但是,我将放置一个静态断言,以便如果传递的数组的大小太大,则在编译时将失败。

Also assuming you are in C++11 here. 还要假设您在这里使用C ++ 11。


I guess depending on your application, this strategy wouldn't be appropriate. 我猜这取决于您的应用程序,不合适。 It might be that the size of the arrays really needs to be variable at run-time, but you still don't want to make dynamic allocations. 可能确实需要在运行时更改数组的大小,但是您仍然不想进行动态分配。 In that case you could have a look at boost::static_vector . 在这种情况下,您可以看看boost::static_vector

http://www.boost.org/doc/libs/1_62_0/doc/html/container/non_standard_containers.html http://www.boost.org/doc/libs/1_62_0/doc/html/container/non_standard_containers.html

boost::static_vector will basically be like a heap-allocated buffer large enough to hold N objects, but it won't default construct N of them for sure, you may have only one or two etc. It will keep track of how many of them are actually alive, and basically act like a stack-allocated std::vector with a capacity limit of N . boost::static_vector基本上就像一个堆分配的缓冲区,足够容纳N个对象,但是肯定不会默认构造N个对象,您可能只有一个或两个,等等。它将跟踪多少个对象。它们实际上还活着,并且基本上像一个堆栈分配的std::vector ,容量限制为N

Use std::copy_n : 使用std::copy_n

std::copy_n(characters, num_characters, this->characters);

Note that the order of arguments is different from memcpy and the number is the number of elements, not the size of those elements. 请注意,参数的顺序与memcpy不同,数字是元素的数量,而不是元素的大小。 You'll also need #include <algorithm> in the top of your source file. 您还需要在源文件顶部#include <algorithm>

That said, you're probably better off using a std::vector rather than a fixed size array, That way you can just use a simple assignment to copy it, and you can grow and shrink it dynamically. 就是说,您最好使用std::vector而不是固定大小的数组,这样,您可以使用简单的赋值来复制它,并且可以动态地对其进行缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM