I’m doing some work in C++ for a company that has everything else written in C (using C isn’t an option for me 🙁 ). They have a number of data structures that are VERY similar (i.e., they all have fields such as “name”, “address”, etc. But, for whatever reason there isn’t a common structure that they used to base everything else off of (makes doing anything hell). Anywho, I need to do a system-wide analysis of these structs that are in memory, and through it all into a table. Not too bad, but the table has to include entries for all the fields of all the variables, even if they don’t have the field (struct b may have field “latency”, but struct a doesn’t – in the table the entry for each instance of a must have an empty entry for “latency”.
So, my question is, is there a way to determine at runtime if a structure that has been passed into a template function has a specific field? Or will I have to write some black magic macro that does it for me? (The problem is basically that I can’t use template specialization)
Thanks! If you have any questions please feel free to ask!
Here’s a snippit of what I was thinking…
struct A
{
char name[256];
int index;
float percision;
};
struct B
{
int index;
char name[256];
int latency;
};
/* More annoying similar structs... note that all of the above are defined in files that were compiled as C - not C++ */
struct Entry
{
char name[256];
int index;
float percision;
int latency;
/* more fields that are specific to only 1 or more structure */
};
template<typename T> struct Entry gatherFrom( T *ptr )
{
Entry entry;
strcpy( entry.name, ptr->name, strlen( ptr->name ) );
entry.index = ptr->index;
/* Something like this perhaps? */
entry.percision = type_contains_field( "percision" ) ? ptr->percision : -1;
}
int main()
{
struct A a;
struct B b;
/* initialization.. */
Entry e = gatherFrom( a );
Entry e2 = gatherFrom ( b );
return 0;
}
First I’d like to quote what Linus Torvalds had to say about this issue:
http://harmful.cat-v.org/software/c++/linus
They may have had very sound reasons for this. Putting common fields into a single base structure (class) may sound like a great idea. But it makes things really difficult if you want to apply major changes to one of the structures (replace some fields, change types, etc.) while leaving the rest intact. OOP is certainly not the one true way to do things.
No this is not possible. Neither in C nor in C++, because all information about types gets discarded when the binary is created. There’s neither reflection nor introspection in C or C++. Well, technically the debug information the compiler emits does provide this information, but there’s no language builtin feature to access this. Also this sort of debug information relies on analysis performed at compile time, not at runtime. C++ has RTTI, but this is only a very coarse system to identify which class an instance is off. It does not help with class or struct members.
But why do you care to do this at runtime anyway?
You should be actually happy that you have to analyse C and not C++. Because C is really, really easy to parse (unlike C++ which is tremendously difficult to parse, mostly because of those darn templates). Especially structs. I’d just write a small and simple script, that extracts all the struct definitions from the C sources. However since structs are of constant size, they often contain pointers to dynamically allocated data. And unless you want to patch your allocator, I think the most easy way to analyse this, is by hooking into a debugger and record the memory usage of every unique object whose pointer is assigned to a struct member.