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?
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.
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.
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.
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).
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With