title: Finding the largest size of 100 structs at compile time in C
body: <p>I have 100 structs that look something like this:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>I want to find the largest <code>sizeof</code> from among these structs at compile time. I tried using a comparison macro like this:</p>
  
  <pre><code> #define LARGER(a, b) ((a) &gt; (b) ? (a) : (b))
  </code></pre>
  
  <p>and then use that to construct the final define which will contain the result:</p>
  
  <pre><code>#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))) /*...*/ ))
  </code></pre>
  
  <p>However, the compiler runs out of space:</p>
  
  <blockquote>
    <p>Error C1060   compiler is out of heap space</p>
  </blockquote>
  
  <p>Which makes sense, given that this <code>#define</code> has to keep track of alot of numbers, since it simply replaces text. To be specific, the number of integers found in <code>MAX_SIZEOF</code> is exponential and can be described as:</p>
  
  <p><a href="https://i.stack.imgur.com/Iuz3X.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Iuz3X.png" alt="number of numbers"></a></p>
  
  <p>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). </p>
  
  <p>I am not sure how I can find an efficient solution in C.</p>
  
  <p>In C++ I could achieve this with <code>constexpr</code> quite easily without any compiling issues whatsoever:</p>
  
  <pre><code>constexpr size_t LARGER(size_t a, size_t b) {
      return a &gt; 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))/*...*/)));
  </code></pre>
  
  <p>But yeah, I have to use C only here. ...Thanks for any ideas!</p>