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
int arraySize = 640000;
needs to be const in standard C++. g++ allows variable length arrays as a C99-inspired language extension. It's best to turn that extension off. :-)
std::vector<unsigned char> vector_buffer;
unsigned char buffer[arraySize];
OK when arraySize is const, but will not compile with e.g. Visual C++ with your original code.
populateArray(&buffer);
This should most probably be populateArray(buffer), unless you have a really weird declaration of populateArray.
for(int i = 0; i < arraySize; i++)
cout << buffer[i]; // this prints out data
The above prints the data with no spacing between the elements. Better add some spacing. Or newlines.
std::copy ( buffer, buffer + arraySize, std::back_inserter(vector_buffer));
Better just use the assign method of std:.vector, like vector_buffer.assign( buffer, buffer + arraySize ).
for(int i = 0; i < arraySize; i++)
cout << vector_buffer[i]; // this prints out different data
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 populateArray function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With