Finding the largest size of 100 structs at compile time in C

  • A+
Category:Languages

I have 100 structs that look something like this:

struct s00 { char   data[30]; }; struct s01 { char   data[30]; }; struct s02 { int    data[10]; }; struct s03 { double data[5];  }; struct s04 { float  data[20]; }; struct s05 { short  data[15]; }; struct s06 { char   data[7];  }; struct s07 { int    data[19]; }; struct s08 { double data[11]; }; struct s09 { float  data[5];  }; struct s10 { char   data[52]; }; //... struct s99 { char   data[12]; };  typedef struct s00 s00; typedef struct s01 s01; typedef struct s02 s02; //... typedef struct s99 s99; 

I want to find the largest sizeof from among these structs at compile time. I tried using a comparison macro like this:

 #define LARGER(a, b) ((a) > (b) ? (a) : (b)) 

and then use that to construct the final define which will contain the result:

#define MAX_SIZEOF (LARGER(sizeof(s00), /                     LARGER(sizeof(s01), /                     LARGER(sizeof(s02), /                     LARGER(sizeof(s03), /                     LARGER(sizeof(s04), /                     LARGER(sizeof(s05), /                     LARGER(sizeof(s06), /                     LARGER(sizeof(s07), /                     LARGER(sizeof(s08), /                     LARGER(sizeof(s09), /                     LARGER(sizeof(s10), /                     //...                     sizeof(s99))) /*...*/ )) 

However, the compiler runs out of space:

Error C1060 compiler is out of heap space

Which makes sense, given that this #define has to keep track of alot of numbers, since it simply replaces text. To be specific, the number of integers found in MAX_SIZEOF is exponential and can be described as:

Finding the largest size of 100 structs at compile time in C

where x is equal the number of structs involved. Assuming 4 bytes for an integer, the compiler would need to allocate 30.4 sextillion terabytes to compute this macro (if my calculations are correct). The maximum my system could handle was 17 structs (786430 numbers, 3.14 megabytes).

I am not sure how I can find an efficient solution in C.

In C++ I could achieve this with constexpr quite easily without any compiling issues whatsoever:

constexpr size_t LARGER(size_t a, size_t b) {     return a > b ? a : b; } constexpr size_t MAX_SIZEOF() { return      LARGER(sizeof(s00), /     LARGER(sizeof(s01), /     LARGER(sizeof(s02), /     LARGER(sizeof(s03), /     LARGER(sizeof(s04), /     LARGER(sizeof(s05), /     LARGER(sizeof(s06), /     LARGER(sizeof(s07), /     LARGER(sizeof(s08), /     LARGER(sizeof(s09), /     LARGER(sizeof(s10), /     //...     sizeof(s99))/*...*/))); 

But yeah, I have to use C only here. ...Thanks for any ideas!

 


You can declare a union of all structs

union allstructs {     struct s00 s00val;     struct s01 s01val;     /* ... */     struct s99 s99val; } 

and get the size of the union

sizeof(union allstructs) 

When you want to transmit structures over the network you should also think about packing/padding of structures, byte order and floating point implementation.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: