Skip to content

Commit

Permalink
use capacity() not size() to get the size of the scratch buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 1, 2023
1 parent 953e132 commit 34fd74e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions six/modules/c++/cphd/source/DataWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ void DataWriterLittleEndian::operator()(std::span<const std::byte> pData, size_t
const auto dataSize = pData.size();
while (dataProcessed < dataSize)
{
// `capacity()`, not `size()`; https://en.cppreference.com/w/cpp/container/vector/capacity
// "Returns the number of elements that the container has currently allocated space for."
// `assign()` (below) will alter `size()` to the number of current elements.
const size_t dataToProcess =
std::min(mScratch.size(), dataSize - dataProcessed);
std::min(mScratch.capacity(), dataSize - dataProcessed);

const auto data = adjust_span(pData, dataProcessed);
const auto begin = data.begin();
const auto end = begin + dataToProcess;
mScratch.assign(begin, end);
mScratch.assign(begin, end); // see above; changes `size()`.

cphd::byteSwap(mScratch.data(),
elementSize,
Expand Down

0 comments on commit 34fd74e

Please sign in to comment.