Reputation: 97
I am trying to copy binary data from a mem buffer to a vector.
Please let me know what is wrong with the folloowing attempts
each time I am getting vector size as 0
int AhaConnectAPI::unpackDataArray(uint8_t* src, vector<uint64_t> dest, uint32_t destvectorSize, int offset)
{
cout<<"\nstd::copy";
std::copy(src + offset, src + offset + sizeof(uint64_t) * length, back_inserter(dest));
return length * 8;
}
or
int AhaConnectAPI::unpackDataArray(uint8_t* src, vector<uint64_t> dest, uint32_t destvectorSize, int offset)
{
uint64_t element;
for(unsigned int i = 0 ; i<destvectorSize; i++)
{
offset = unpackData(src, element, offset);
dest.push_back(element);
}
return dest.size() * sizeof(uint64_t);
}
Upvotes: 2
Views: 1425
Reputation: 18421
Try this:
int AhaConnectAPI::unpackDataArray(uint8_t* src, vector<uint64_t>& dest,
uint32_t destvectorSize, int offset)
You need to pass vector
by reference (note the &).
Upvotes: 1
Reputation: 27577
In your first unpackDataArray()
: your src
is of type uint8_t*
but you use sizeof(uint64_t)
in std::copy
and for dest
, and offset
is an int
. Try something like:
std::copy(static_cast<uint64_t*>(src) +
static_cast<uint64_t>(offset),
static_cast<uint64_t*>(src) +
static_cast<uint64_t>(offset) + sizeof(uint64_t) * length,
back_inserter(dest));
offset
may need further adjustment. Also length
appears out of nowhere.
Upvotes: 3