-1

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?

3
  • The "containing just a few integers" part fights a bit with the "thousands to hundreds of thousands of Data objects."
    – Andy Prowl
    Mar 29, 2013 at 18:36
  • Each 'class Data' object holds 4 dimensions of integers. There exist many instances of Data across many time points. Is there something off about that? Mar 29, 2013 at 18:37
  • Oh, all right, I got that. Thank you :)
    – Andy Prowl
    Mar 29, 2013 at 18:40

2 Answers 2

2

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.

4
  • So a shared_ptr to a vector is unnecessary as anything but a wrapper. That is very helpful to know. Mar 29, 2013 at 18:38
  • Vectors usually do not distingiush between pointers and non-pointers in deciding whether to store vector elements on the heap or not
    – Oswald
    Mar 29, 2013 at 18:39
  • true but a pointer could be a pointer to the stack or heap and as such when added to the vector it may not be on the heap. But the pointer itself would be on the heap as you point out @Oswald
    – spartacus
    Mar 29, 2013 at 18:40
  • If a pointer is a pointer to a stack object and you add it to a vector, then you add the pointer to the vector, not the pointed to object.
    – Oswald
    Mar 29, 2013 at 18:41
2

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

If you need reference semantics, i.e. if you need the values in the container to be aliases for values which are also referred to from other parts of the code, and modifications made in one part of the code should be visible for other parts that hold an alias to the modified Data object, I would say this is the right container definition:

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

For what concerns your question about where the storage comes from, std::vector always allocates its elements dynamically in a continuous region of storage, no matter whether those are shared_ptrs, vectors, or Datas.

However, I would recommend you to think if you really need reference semantics, or if it is not enough to store objects of type Data by value inside the containers:

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

This would simplify your code and you would also get rid of the shared_ptr memory and run-time overhead.

Whether or not you need reference semantics is something that only you, as the designer of your application, can tell. The information you provided is not enough for me to tell it without uncertainty, but hopefully this answer gave you a hint on the kind questions you should ask yourself, and what would be the answer in each case.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.