Dynamic Structures
3 messages in this thread
Is there a way to dynamically create a structure? A custom structure. The
program user decides on the elements of the structure, and the program
creates a structure on the fly, at run time, not compile time. I suppose
you could get the elements from the user, and allocate memory for the
entire struct, and then create the appropriate type pointers to each of the
elements (offset from the start of the allocated memory)… but is there an
easier or more straightforeward way? –Eet–
Director of Mayhem
Extra Terrestrial Imports, Ltd.
That is the easy and straigtforward way. 🙂 Why call it a "structure"?
You certainly couldn't reference the elements with element names, as you
can with a true C structure. Think of it as sort of a data packet.
Chances are, a tagged structure – think IFF – would get you what you
want… Make each block an arbitrary series of #define or 'enum' values,
followed by a length (if it's not implicit in the data type) and then the
data itself. Later, to fetch the data, parse the tags and skip data until
you get to the thing you want. What are you writing?
There's no easy way to do this in C. There's probably a million and a half
ways it could be done, but none of them are straight forward. It would
probably be fairly simple to do in C++ using late binding mechanisms
though. However, my question to you is, why do you want to do this?
-Mike