2

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

6
  • This is most troubling. Is this an output error, or does comparing the buffer and vector byte-to-byte identify differences? If so, what are they? Do they follow any pattern, such as regular offsets or only some values being affected? Commented Oct 13, 2010 at 17:33
  • The code as presented should work. Is that the real code that is failing? Are the types of the array and the vector the same? --Note that you can use (and might be more efficient) vector_buffer.insert( buffer, buffer+arraySize ), but that is unrelated to the problem at hand. Commented Oct 13, 2010 at 17:35
  • Can you try with array_size being 10 or 20 and show us the putput? Also I am not sure unsigned char buffer[arraySize] can compile on all compilers. Commented Oct 13, 2010 at 17:37
  • 1
    what is signature of populateArray? Commented Oct 13, 2010 at 17:38
  • Is it unsigned char ** or unsigned char *? Commented Oct 13, 2010 at 17:40

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
3

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.

Comments

1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.