The overload of the constructor of std::vector
that you are using takes the number of elements to initialize the vector with and initializes these (to 0
in this case).
After the line
std::vector<unsigned char> *data = new std::vector<unsigned char>(size);
*data
therefore contains already size
elements set to zero.
Then with data->push_back(...)
you are adding additional elements after these size
elements. You are not overwriting the previous ones.
Either use
std::vector<unsigned char> *data = new std::vector<unsigned char>();
to default-initialize an empty vector, or use
(*data)[transfer] = ...
to set the elements at the given location.
Furthermore your program has undefined behavior if the fread
reads less than size
bytes into the array. You need to check the number of bytes read from its return value and you are not allowed to access any elements beyond that in data
, because you never initialized it.
You can initialize it to zero with:
unsigned char* buffer = new unsigned char[size]{};
If you want to write C++, don't use C library functions like fread
, use the facilities provided by <fstream>
, i.e. std::ifstream
and std::ofstream
instead.
Similarly there is no need for dynamic memory allocation here. Declare variables with automatic storage:
unsigned char buffer[size]{};
std::vector<unsigned char> data(size);
and the rest of the syntax also simplifies:
data[transfer] = ...
Finally, as mentioned in the other answer, there is a constructor for std::vector
that will perform the whole copy loop for you. Note however that my argument about undefined behavior still applies when using that.
Defining data
as automatic array as in
unsigned char buffer[size]{};
works only if size
is a compile-time constant. If it is not, then this part of my advice does not apply. However there is no real need to use arrays at all in any case. You can initialize a std::vector
of proper size (compile-time constant or not) and provide that as buffer via its .data()
method, which returns a pointer to the underlying (continuous) storage:
std::vector<unsigned char> buffer(size);
fread(buffer.data(), sizeof(unsigned char), buffer.size(), f);