Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable-length objects: Ever a good idea?

My application uses a large amount of Panda objects. Each Panda has a list of Bamboo objects. This list does not change once the Panda is initialized (no Bamboo objects are added or removed). Currently, my class is implemented as follows:

class Panda
{
    int a;
    int b;
    int _bambooCount;
    Bamboo* _bamboo;

    Panda (int count, Bamboo* bamboo)
    {
        _bambooCount = count;
        _bamboo = new Bamboo[count];

        // ... copy bamboo into the array ...
    }
}

To alleviate the overhead of allocating an array of Bamboo objects, I could implement this class as follows -- basically, instead of creating objects via the regular constructor, a construction method allocates a single memory block to hold both the Panda object and its Bamboo array:

class Panda
{
    int a;
    int b;

    Panda ()
    {
        // ... other initializations here ...
    }

    static Panda *createPanda (int count, Bamboo* bamboo)
    {
        byte* p = new byte[sizeof(Panda) +
                           sizeof(Bamboo) * count];
        new (p) Panda ();

        Bamboo* bamboo = (Bamboo*)
            p + sizeof(Panda);

        // ... copy bamboo objects into the memory
        // behind the object...

        return (Panda*)p; 
    }
}

Can you foresee any problems with the second design, other than the increased maintenance effort? Is this an acceptable design pattern, or simply a premature optimization that could come back to bite me later?

like image 415
Tony the Pony Avatar asked Sep 07 '09 16:09

Tony the Pony


People also ask

What are the advantages opting for variable length arrays in C?

The benefit of using a variable length array (VLA) in C is that you can size the array at run time based on a variable or parameter value.

Does C++ support VLA?

VLA is a C99 feature but not extended to C++, because C++ says vectors almost always better then VLA, it's probably too much of work for compiler writers to implement them for the rare cases where they might be better than vectors. nevertheless some compilers like gcc supports them through compiler extensions.

Are variable sized arrays allowed in C++?

A variable length array cannot be initialized. Note: In C++ applications, storage allocated for use by variable length arrays is not released until the function they reside in completes execution. A variable length array can be the operand of a sizeof expression.

What makes an array a variable length array?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).


2 Answers

C++ gives you another option. You should consider using std::vector.

class Panda
{
    int a;
    int b;
    std::vector<Bamboo> bamboo;
    // if you do not want to store by value:
    //std::vector< shared_ptr<Bamboo> > bamboo;

    Panda (int count, Bamboo* bamb) : bamboo( bamb, bamb+count ) {}
}

If you want to store Panda and Bamboos in continuous memory you could use solution from this article. The main idea is to overload operator new and operator delete.

like image 170
Kirill V. Lyadvinsky Avatar answered Sep 28 '22 23:09

Kirill V. Lyadvinsky


How do we convince people that in programming simplicity and clarity --in short: what mathematicians call 'elegance'-- are not a dispensable luxury, but a crucial matter that decides between success and failure?

-- Edsger W. Dijkstra

like image 31
2 revs Avatar answered Sep 28 '22 21:09

2 revs