Reputation: 11399
I would like to fill a struct using memcpy.
The struct is declared like this:
struct udtFeatures
{
vector<unsigned char>ByteFeatures;
};
And this is where I would like fill the bytes:
void clsMapping::FeedFeaturesFromMap(udtFeatures &uFeatures,int uOtherIndex)
{
int iBytePos=this->Content()[uOtherIndex].ByteStart;
int iByteCount=this->Content()[uOtherIndex].ByteCount;
memcpy(uFeatures.ByteFeatures, &((char*)(m_pVoiceData))[iBytePos],iByteCount);
}
However memcpy does not like this.
The compiler says:
No matching conversion function found for
std::vector<unsigned char, std::allocator<unsigned char>> in void *.
I guess it is because it .ByteFeatures is just a pointer?
How could I do this?
Upvotes: 1
Views: 3318
Reputation: 254581
I guess it is because it .ByteFeatures is just a pointer?
No, it's because it isn't just a pointer. It's a vector.
How could I do this?
If you want a pointer to the array managed by the vector, then that's uFeatures.ByteFeatures.data()
or &uFeatures.ByteFeatures[0]
. Alternatively, you might consider using std::copy
instead.
In either case, make sure the vector is large enough before copying things into it.
Upvotes: 5
Reputation: 9527
You cant fill std::vector
using memcpy (Well.. there is a way, but for you is better to think it as if not). Use std::copy
or fill it by yourself.
Upvotes: 0
Reputation: 409256
It would be possible to use memcpy
to initialize a std::vector
, but not advisable.
Instead use std::copy
:
std::copy(&m_pVoiceData[iBytePos], &m_pVoiceData[iBytePos + iByteCount],
std::back_inserter(uFeatures.ByteFeatures));
Upvotes: 0