I have a class containing a set of C dynamic arrays which are all of the same size, with one of the members being the size of these arrays :
class stuff {
int size;
float *abc;
float *def;
// etc.
};
To ease memory management of this class (and a lot of other classes having a similar layout), I would like to use standard containers. However I must keep the alignment constraint provided by C arrays (and vectors) because this data will be passed around in a lot of C functions.
My problem here is that each vector has its own size, which is redundant and can lead to errors if for one reason or another one changes its size and not the others.
Is there a way to force the arrays/vectors/whatevers to always be the same size ?
This is perhaps one case where it make sense to create a class of your own to manage the memory (no business logic, just memory).
You may, of course, articulate this class around vectors:
We make sure to always check for the class invariant (in mutating methods) to ensure that it always apply.
Note: there is a slight flow here, if
_b.push_back(b);throws, then_awas extended and_bwas not, this can be handled in a try/catch block OR as SteveJessop aptly remarked, by (1) reserving enough space beforehand in both vectors and (2) pushing in them.