Search code examples
c++pointersvectorc++11shared-ptr

Basic - shared_ptr to vector of vectors of values


I have a vector of a vectors of objects containing just a few integers.

The outer vector holds hundreds of vectors, those hold thousands to hundreds of thousands of Data objects.

I am using a library with a lot of shared_ptr's involved, so that's what i'll be using.

How do I store this so that the data is stored to the heap?

std::vector<std::shared_ptr<std::vector<Data>>>
std::vector<std::vector<std::shared_ptr<Data>>>

etc

What is the correct way to handle this?


Solution

  • To store something on the heap you use new in c++ or malloc in c. Although I believe that the vector implementation does use the heap since vector is a dynamically sized container. So in reality if you add an element to a vector that elemenet is already on the heap unless it is a pointer in which case just the pointer is on the heap and not the element that the pointer points to as @Oswald points out.