It'sPete
It'sPete

Reputation: 5211

Memcpy a 2-d vector

Long story short, I have a 2-d vector that I need to memcpy(). How do I get a pointer to the first element of the data structure?

Also, given that it's in continuous memory, I can just pass in this pointer and the total size, correct?

Would the following code work?

vector < vector <int> > vec;
//  .. assume it's populate and built up here
int* ptr = vec[0].data();  // use this pointer

Upvotes: 0

Views: 867

Answers (3)

TheUndeadFish
TheUndeadFish

Reputation: 8171

You can't memcpy the contents because you don't really have a single contiguous array. You have data made up of many parts.

The outer vector manages its internal array. That array is made up of the inner vector objects who each manager their own separate internal arrays of ints. So that's not just one chunk of memory for the whole thing.

To try to visualize it:

vector<vector<int>> object
                       |
                       |-----> internal array:
                                            vector<int>object
                                                        |
                                                        |------>  internal array:
                                                                                int
                                                                                int
                                                                                int
                                            vector<int>object
                                                        |
                                                        |------>  internal array:
                                                                                int
                                                                                int
                                                                                int
                                            vector<int>object
                                                        |
                                                        |------>  internal array:
                                                                                int
                                                                                int
                                                                                int

Each internal array there exists at a different location in memory, in no way adjacent to each other.


However depending on what you're doing and where you need to copy the data do, you might be able to iterate through all the sub-vectors and memcpy each of their contents sequentially to the destination.

Upvotes: 0

Paul Evans
Paul Evans

Reputation: 27577

No, only each vector<int> has contiguous memory. So you what something like:

vector < vector <int> > vec;
// ...
int** ptr = new *int[vec.size()];
for (int i = 0; i < vec.size(); ++i) {
    ptr[i] = &vec[i][0];
}

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308391

You don't have a single 2-D vector, you have a vector that contains other vectors. Each vector, considered individually, has contiguous storage, but they don't adjoin each other - there's no way to do what you want with a single copy.

You can use the Boost Multi-dimensional Array Library to treat a single vector as a 2-D one, which would then allow you to get the entire contents at once.

Upvotes: 6

Related Questions