Im trying to copy an array to a vector, however, when the data is copied to the vector its different from that of the original array.
int arraySize = 640000;
std::vector<unsigned char> vector_buffer;
unsigned char buffer[arraySize];
populateArray(&buffer);
for(int i = 0; i < arraySize; i++)
cout << buffer[i]; // this prints out data
std::copy ( buffer, buffer + arraySize, std::back_inserter(vector_buffer));
for(int i = 0; i < arraySize; i++)
cout << vector_buffer[i]; // this prints out different data
The data seems to get compressed somehow. Any approach at copying the array to a vector does the same thing.
Im using it to create a video from images. If i use the array data all is well, but if i use the vector data it doesn’t work.
Any help would be highly appreciated.
Cheers
The
needs to be
constin standard C++. g++ allows variable length arrays as a C99-inspired language extension. It’s best to turn that extension off. 🙂OK when
arraySizeisconst, but will not compile with e.g. Visual C++ with your original code.This should most probably be
populateArray(buffer), unless you have a really weird declaration ofpopulateArray.The above prints the data with no spacing between the elements. Better add some spacing. Or newlines.
Better just use the
assignmethod ofstd:.vector, likevector_buffer.assign( buffer, buffer + arraySize ).Again, this displays the elements with no spacing between.
Is the apparent problem there still when you have fixed these things?
If so, then please post also your
populateArrayfunction.