Question

i have 2 classes working with communication data in one class the array information is stored as a fixed array

uint8 msg[512];   
//fixed array to reduce mem fragmentation on target

i want to memory copy this to a vector in the 2nd class

vector<uint16> schbuff;  
// not set offend so mem fragmentation on such a issue

i am using c++ c99 so and Im looking for a way not to have pass each element to the new container

if they are both the same type it would not be such a issue but imm new to c++ so any help would be appreciated

Was it helpful?

Solution

You can use std::copy:

#include <algorithm>   // for copy
#include <iterator>    // for begin, end, back_inserter

std::copy(std::begin(msg), std::end(msg), std::back_inserter(schbuff));

OTHER TIPS

You can use constructor

vector<uint16> schbuff( msg, msg + 512 );   

Or can use member function assign

schbuff.assign( msg, msg + 512 );   

Or can use member funcrion reserve. For example

vector<uint16> schbuff;
schbuff.reserve( 512 );

std::copy( msg, msg + 512, std::back_inserter( msg ) );
const int msg_size = 512;
uint8 msg[msg_size];
vector<uint8> msg_vec;
msg_vec.assign(msg, msg + msg_size);

Edit: just noticed the types are different. But I will keep the answer for example of how to put in vector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top