I have a case where there are multiple structures contained in one structure. Small example:
typedef struct data {
int x;
} data_t;
typedef struct info {
data_t data_element[10];
int y;
} info_t
Is there any way to print all struct sizes which are members of info_t
?
CodePudding user response:
Not without knowing what those types are. C does not have reflection (the way that java or c# do, for example).
If you do know the types it's simply a matter of using sizeof and a print function.
CodePudding user response:
This is impossible using the C language alone, since it does not provide for such introspection. All you can do is iterate over the struct members and print their sizes.
That said, a decade ago, I had to do just this for large complicated struct of several hundred members. I finally got the required information from parsing the debugging section of the object file. I don't have details anymore, but you will need to compile with the -g
flag and then dump debug output with an appropriate tool. Try looking at the readelf
and objdump
utilities and manual pages.