openlang

Can I display the sizes of every nested structure member in C?

My situation involves a structure that holds several other structures inside it. Here's a small illustration:

typedef struct payload {
    int offset;
} payload_t;

typedef struct record {
    payload_t payloads[12];
    int status;
} record_t

Can I print the sizes of every structure belonging to record_t?

The conversation continues 2

Follow the reasoning
  1. Permalink

    You'd first have to know the relevant types. Unlike java or c#, for instance, C offers no reflection.

    Once you know those types, you just need sizeof together with a function that prints the result.

  2. Permalink

    The C language by itself can't do this, because it lacks that kind of introspection. Your only option is to visit each structure member and output its size.

    However, eight years back I faced exactly this task with a big, complicated struct containing four hundred members. Eventually I extracted what I needed by parsing the object file's debugging information. I've forgotten the specifics, but you'll have to compile using the -g option, then use a suitable tool to dump the debug information. The readelf and objdump tools and their manuals would be worth checking.

Other sides of the same question

  1. Using MATLAB through Google Colab

    Can MATLAB commnads be used from Google Colab (cloud-hosted Jupyter)? For instance, I'd like to invoke Risk Management Toolbo…

    Replies 2Latest activity matlab
  2. Can gdb set a breakpoint for an assembly instruction using its address?

    My attempt: apparently this command does not exist, though. Can gdb do something like this?

    Replies 2Latest activity assembly
  3. Can Playwright assertions allow either of two possible text values?

    I'd like to verify that the text contains either one or two: Can this be made to work in Playwright? Could the same thing als…

    Replies 2Latest activity typescript