views:

116

answers:

3

Hi, 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

+3  A: 

I can see nothing wrong with your code. The following code

#include <iostream>
#include <vector>

int main()
{
    const std::size_t arraySize = 640000;

    unsigned char buffer[arraySize];

    for(std::size_t idx = 0; idx < arraySize; ++idx)
        buffer[idx] = idx;

    std::vector<unsigned char> vector_buffer(buffer, buffer + arraySize);
    //std::vector<unsigned char> vector_buffer;
    //std::copy (buffer, buffer + arraySize, std::back_inserter(vector_buffer)); 

    for(std::size_t idx = 0; idx < arraySize; ++idx)
        if( buffer[idx] != vector_buffer[idx] )
        {
            std::cout << "error @" << idx << '\n';
            return 1;
        }
    std::cout << "Ok.\n";

    return 0;
}

prints Ok. for me. (Even if I use the less-than-optimal way of copying into the vector.)

From the fact that the code you showed wouldn't compile I conclude that you're not showing the real code. Please do so. Somewhere in the differences between your real code and my code must be the problem.

sbi
+6  A: 

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.

Alf P. Steinbach
+1 for using `assign()`. I suspect that, in OP's code, `vector_buffer` is somehow getting modified between its declaration and the call to `std::copy()`. If possible, even better is to delay the `vector_buffer` construction until `buffer` is ready, and then do `vector_buffer(buffer, buffer + arraySize);` as in @sbi's code.
ArunSaha
+1  A: 

I've written a complete compilable program for you. The code appears fine. I run it and get expected output. Perhaps you need to re-check the code you posted against the real code.

#include <cstdlib>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;

void populateArray(unsigned char* buf, size_t buf_size)
{
    unsigned char* buf_end = &buf[buf_size];
    for( unsigned char c = 'A'; buf != buf_end; c = (c=='Z'?'A':c+1), ++buf )
        *buf = c;
}

int main()
{

    static const int arraySize = 64;

    std::vector<unsigned char> vector_buffer;
    unsigned char buffer[arraySize];

    populateArray(buffer, sizeof(buffer));

    for(int i = 0; i < arraySize; i++)
             cout << buffer[i];  // this prints out data

    cout << endl;


    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   

    return 0;
}
John Dibling