简体   繁体   中英

Retrieve pointer that can acess all the elements of arrays stored in a std::vector

Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?

For example (see below please), here std::vector<unsigned char*> vec contains two differently sized unsigned char* pointers. I need to have a buffer pointer that points to all pushed data in this vector. I'd guess that this is possible as the standard guarantees that a vector uses a contiguous memory buffer, right?

PS are the two ways of printing the elements of the vector I use in this example fine? (the two for loops)

unsigned char* data1 = new unsigned char[3];
data1[0] = 'a';
data1[1] = 'b';
data1[2] = 'c';


unsigned char* data2 = new unsigned char[1];
data2[0] = 'x';

std::vector<unsigned char*> vec;
vec.push_back(data1);
vec.push_back(data2);

for (size_t i = 0; i < vec.size(); i++) {
    std::cout << vec[i];
}

std::cout << "\n";

for (auto iter = vec.begin(); iter != vec.end(); iter++) {
    std::cout << (*iter);
}
std::cout << "\n\n";


unsigned char* buffer = (unsigned char*) vec[0];

Does buffer point to all data in vec ? ie buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x ?

Does buffer point to all data in vec ? ie buffer[0] = a, buffer[1] = b, buffer[2] = c, buffer[3] = x ?

It doesn't. It points to the beggining of the array stored in first element of the vector.

Are the two ways of printing the elements of the vector I use in this example fine?

They are not, those arrays are not null terminated, they can't be printed as strings.

Is it possible to have a pointer that points to the contiguous buffer that is used by a vector?

Yes, it's possible.

If you'd like a pointer that can correctly access all the data in the vector, including individual elements of the unsigned char array members you'd want:

unsigned char **buffer = vec.data();

And the access:

for(size_t i = 0; i < 3; i++)
    std::cout << buffer[0][i]; //indexing like a 2D array, albeit unbalanced
                                 //output: abc

std::cout << buffer[1][0]; //output: x

Note that I use a cycle to access each element of data1 instead of simply treating it like a string, and this is because it is not a string, aka a null terminated char array.

Needless to say that you will need to know how many elements are stored in each array.

Alternatively you can null terminate them:

unsigned char* data1 = new unsigned char[4];
//...
data1[3] = '\0';

And

unsigned char* data2 = new unsigned char[2];
//...
data2[1] = '\0';

Here printing them like strings:

std::cout << buffer[0];
std::cout << buffer[1];

Using a null terminator has the extra benefit of allowing you to know the size of the arrays at any time using strlen((char*)buffer[0]) .

You want the data() method on vector. It will return a pointer to the data, assuming that the vector size is greater than zero. If it is zero, then data() will return something but using it is undefined.

Read https://en.cppreference.com/w/cpp/container/vector/data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM