Home > front end >  How can I pass and store an array of variable size containing pointers to objects?
How can I pass and store an array of variable size containing pointers to objects?

Time:12-22

For my project I need to store pointers to objects of type ComplicatedClass in an array. This array is stored in a class Storage along with other information I have omitted here.

Here's what I would like to do (which obviously doesn't work, but hopefully explains what I'm trying to achieve):

class ComplicatedClass
{
    ...
}


class Storage
{
    public:
        Storage(const size_t& numberOfObjects, const std::array<ComplicatedClass *, numberOfObjects>& objectArray)
            : size(numberOfObjects),
              objectArray(objectArray)
        {}

    ...

    public:
        size_t size;
        std::array<ComplicatedClass *, size> objectArray;
    
    ...
}


int main()
{
    ComplicatedClass * object1 = new ComplicatedClass(...);
    ComplicatedClass * object2 = new ComplicatedClass(...);
    Storage myStorage(2, {object1, object2});
    
    ...
    
    return 0;
}

What I am considering is:

  1. Using std::vector instead of std::array. I would like to avoid this because there are parts of my program that are not allowed to allocate memory on the free-store. As far as I know, std::vector would have to do that. As a plus I would be able to ditch size.
  2. Changing Storage to a class template. I would like to avoid this because then I have templates all over my code. This is not terrible but it would make classes that use Storage much less readable, because they would also have to have templated functions.

Are there any other options that I am missing?

CodePudding user response:

How can I pass and store an array of variable size containing pointers to objects?

By creating the objects dynamically. Most convenient solution is to use std::vector.

size_t size;
std::array<ComplicatedClass *, size> objectArray;

This cannot work. Template arguments must be compile time constant. Non-static member variables are not compile time constant.

  1. I would like to avoid this because there are parts of my program that are not allowed to allocate memory on the free-store. As far as I know, std::vector would have to do that.

std::vector would not necessarily require the use of free-store. Like all standard containers (besides std::array), std::vector accepts an allocator. If you implement a custom allocator that doesn't use free-store, then your requirement can be satisfied.

Alternatively, even if you do use the default allocator, you could write your program in such way that elements are inserted into the vector only in parts of your program that are allowed to allocate from the free-store.

I thought C had "free-store" instead of heap, does it not?

Those are just different words for the same thing. "Free store" is the term used in C . It's often informally called "heap memory" since "heap" is a data structure that is sometimes used to implement it.

CodePudding user response:

Beginning with C 11 std::vector has the data() method to access the underlying array the vector is using for storage.

And in most cases a std::vector can be used similar to an array allowing you to take advantage of the size adjusting container qualities of std::vector when you need them or using it as an array when you need that. See https://stackoverflow.com/a/261607/1466970

Finally, you are aware that you can use vectors in place of arrays, right? Even when a function expects c-style arrays you can use vectors:

vector<char> v(50); // Ensure there's enough space
strcpy(&v[0], "prefer vectors to c arrays");
  • Related