I am processing several chunks of byte arrays (uint_8) and append them to a string (my_string). For efficiency purposes, I have reserved more than enough bytes for my string by
my_string.reserve(more_than_enough_bytes);
I am trying to append each chunk as shown in the following function:
bool MyClass::AppendToMyString(uint_8* chunk, size_t chunk_num_bytes) {
memcpy(const_cast<uint_8*>(my_string.data()), chunk, chunk_num_bytes);
return true;
}
But the problem is that memcpy does not update my_string size. So, next time when this function is called, I do not where the last element was, other than using a separate variable for it. Any ideas?
my_string.append(chunk, chunk_num_bytes);You'll probably need a cast fromuint_8*tochar*memcpy(my_string, chunk, chunk_num_bytes);compiles. What type ismy_string? I've been assumingstd::stringbut maybe not.std::string::append. Overload 4 from that page should optimize to amemcpyfor you.string'ssizeandlengthmethods are guaranteed to be constant complexity. This means when you callsize,stringcannot run off and count the number of characters before the first null. It needs to be able to set the size as thestringis modified. Sincememcpydoesn't know it's operation on astring, it just sees an address it's supposed to write to, it cannot update thestring's size.