简体   繁体   中英

How do I copy arrays that contain non primitive types?

I'm writing C++ on an Arduino. I've run into a problem trying to copy and array using 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(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. 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 . 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.

您将必须分别复制成员,或在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. You could concievably use std::array instead, and just use copy constructor of 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 ? 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.

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. 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.


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 .

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 .

Use 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. You'll also need #include <algorithm> in the top of your source file.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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