nikolas
nikolas

Reputation: 8975

How to handle large pointer structures

I writing an application that pushes data over an external service to some server. There is a defined structure that I have to use for this, and it looks like this:

class A {
    B *b; // Another class built the same principle
    C *c; // And another one
    ...
};

and it somewhere boils down to

class XY {
    double *some_val;
    std::string *another_val;
    ...
};

The problem I am facing is that I am only dealing with pointers of pointers of pointers if you will, and I haven't figured out yet what would be the best way of dealing with these structures.

I could handle memory management myself with new and delete, but I would have to do massive cleanup afterwards and chances that I forget to clean something up and my program leaks are too high in my opinion.

I could declare the classes locally and fill them up that way, but I would have to declare every variable beforehand then, so I have to analyze the class hierachy before I can do anything and after that it will still get really messy.

Smart pointers came in my mind as well, but since they are basically a class wrapping a pointer I would have to declare them beforehand as well, the advantages over the previous method seem to be small.

Does anyone know a smart and, most of all, readable/maintainable method to handle these massive pointer structures?

Update:

If anyone runs into the same problem, this is my approach now (though I still think it's messy!):

#include <boost/shared_ptr.hpp>
#include <boost/any.hpp>

class A {
    B *b_;
    double *d_;
};

int main() {
    std::vector<boost::any> vp;

    // Create instance of A
    vp.push_back(boost::shared_ptr<A>(new A));
    A *a = boost::any_cast<boost::shared_ptr<A> >(vp.back()).get();

    // Create instance of B in A
    vp.push_back(boost::shared_ptr<B>(new B));
    a->b_ = boost::any_cast<boost::shared_ptr<B> >(vp.back()).get();

    // etc.

    return 0;
}

Update 2:

#include <boost/shared_ptr.hpp>

typedef std::vector<boost::shared_ptr<void> > MemoryManager;

template <class T> T* create_managed(T *ptr, MemoryManager &memorymanager) {
    memorymanager.push_back(boost::shared_ptr<T>(ptr));
    return boost::static_pointer_cast<T>(memorymanager.back()).get();
}

int main() {
    MemoryManager mm;
    A *a = create_managed(new A, mm);
    a->b_ = create_managed(new B, mm);
    return 0;
}

Upvotes: 0

Views: 168

Answers (2)

Alecs
Alecs

Reputation: 2316

Smart pointers is what you need. There are a lot of their implementation, or you can write your own. Using them in proper way you don't have to care about deleting your classes.

Upvotes: 1

karlphillip
karlphillip

Reputation: 93410

Consider using smart pointers, they have a big advantage:

To be smarter than regular pointers, smart pointers need to do things that regular pointers don't. What could these things be? Probably the most common bugs in C++ (and C) are related to pointers and memory management: dangling pointers, memory leaks, allocation failures and other joys. Having a smart pointer take care of these things and can save a lot of aspirin...

Upvotes: 5

Related Questions