Is it possible at runtime to dynamically create a struct? I don't mean dynamic array size, I mean the variables and such inside a structure.
Name:
Anonymous2011-12-06 2:50
A struct serves a mapping from member names to values in the struct. In C, a struct is efficiently implemented as a block of memory that has been reserved, say of size n bytes. There is an associated mapping from member names to offsets in the struct: f: {names} -> {offsets}. When you use structs in C, the function f is defined at compile time, and it is applied to the member names at compile time to determine offsets into a struct to be used at runtime. If you would like to dynamically create and use structure definitions at run time, then you will need to manage the mapping, f with some kind of implementation that will exist at run time. Basically, you are asking if it is possible to have a data structure where given string keys, you can get values. And yes, there are many choices you can use for this, like hash maps, or trees. You could try to compactify the data structure into something like a C struct, where you have a chunk of memory with offsets into it reserved for certain values. You could then manage the size of the block of memory and the mapping to the offsets using another data structure, or maybe a function. If you wanted to, you could say that an array is like a struct, but the member variable names are numbers, instead of strings.