I’m trying to parse returned information that can have a variable amount of data in a structure. I’m not sure how to do this efficiently, I wrote a class that contains each variable as a function and that function returns the data by calculating the appropriate offset however its not very manageable, there must be a better way. I’ve read about vectors (not much experience with them) but when I add them to a structure it adds additional padding which shifts all the variables over.
for example:
struct info_t {
UINT32 signature;
UINT32 elements[NUM_ELEMENTS];
UINT32 options;
};
NUM_ELEMENTS is dynamically generated, and only known at runtime, the elements variable must be exactly NUM_ELEMENTS in size or the options variable will have the wrong offset.
I’m happy if I can initialize the structure pointer when its needed, but C++ won’t let me get past having an unknown NUM_ELEMENTS variable during compile. Any ideas? Thanks.
I assume that
info_tis the format of the data you are trying to parse, so you can’t change it.Is there any particular reason why using the wrapper class isn’t manageable? It seems like exactly the sort of problem where OOP is useful. If you wan’t a class where the interface is compatible with
info_tyou could use the following layout:You must then create a constructor which sets the fields to point to the correct locations in your input data.
If your input data is given as a pointer to an array of UINT32s, the constructor could look something like this:
If you want
datato be deallocated when theinfo_tinstance goes out of scope you can do that in the constructor.