bearophile bearophileHUGS at lycos.com
Mon Apr 19 20:11:46 PDT 2010

Ellery Newcomer:

> Won't the union balloon Wrapper's size to always be that of the largest 
> inner struct that you use?

Beside using the array of struct-enums, or using an array of pointers I've explained in the other answer, there are other solutions, but they are even more complex, there are ways to create variable-sized items inside the "array" that becomes more like a memory arena for the structs.

Then you need ways to find your structs inside this array, there are many ways to do this, with various compromises between memory used and retrieval performance.

The minimum extra memory comes from scanning this arena linearly, because the tags tell you how much big each struct is, this has O(n) search. The max extra memory comes storing a second array of starting pointers that give you O(1) search. An intermediate solution is for example to add a skip list inside the array, with O(ln n) both in extra space and access time, etc.

But in most situations all this work is not necessary, unless you are in needs of space and performance are very demanding...


More information about the Digitalmars-d-learn mailing list