3

I want to append the raw bytes into vector like this.

vector.reserve(current_size + append_data_size);
memcpy(append_data, vector.data() + current_size, append_data_size);
vector.resize(current_size  + append_data_size) // Expect only set size to current_size + append_data_size.

does below is slower? because I think vector is initialised to default first then set the data which is waste.

vector.resize(current_size  + append_data_size);
memcpy(append_data, vector.data() + current_size, append_data_size);
4
  • 2
    When resize enlarges a vector, it creates new values (even if there is already space for them). The reserve operation just makes space. Commented Feb 11, 2020 at 14:47
  • 3
    If you want to add data to a vector, use its interface. For instance vector.insert(iter_pos_into_vec, start_of_range_to_copy, end_of_range_to_copy) Commented Feb 11, 2020 at 14:49
  • You say you want to append, but you're using memcmp() for that!? Also, what is n? Why would n + 100 become 100? Commented Feb 11, 2020 at 15:03
  • Now, check the arguments of memcpy(). That is exactly why you're required to provide a minimal reproducible example, btw. It avoids code that doesn't compile and doesn't work, thus obfuscating the question. Commented Feb 11, 2020 at 15:08

2 Answers 2

10

Modifying vector storage beyond its size is undefined behavior, and a subsequent resize will initialize the new elements at the end of the storage.

However, you could use insert instead:

vector.insert(vector.end(), bytes, bytes + size);
Sign up to request clarification or add additional context in comments.

2 Comments

Mind you, this will convert from decltype(*bytes) to vector::value_type, whereas the memcpy will copy raw bits.
Right. And where insert is not the same as memcpy, I bet you want insert.
5

Even if you call reserve, you still must call resize on the vector if you want to access the new elements, otherwise the behaviour of your code is undefined. What reserve can do is make push_back and other such operations more efficient.

Personally I wouldn't concern yourself with any such optimisations unless you can prove they have an effect with an appropriate profiling tool. More often than not, fiddling with the capacity of a std::vector is pointless.

Also using memcpy is hazardous. (Copy constructors will not be called for example, knowledge of the exact behaviour of copying padding in structures with memcpy for example is a sure way of increasing your reputation on this site!) Use insert instead and trust the compiler to optimise as appropriate.

Without an explicit additional parameter, std::vector::resize value-initialises any additional members. Informally that means the elements of a std::vector of T say are set to values in the same way as the t in static T t; would be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.