title: Array of size 1 in C - why?
body: <p>I'm trying to understand how memory is managed in the <a href="https://gmplib.org/" rel="nofollow noreferrer">GMP</a> project. For reference, the GMP documentation gives the following example for how their <code>mpz_t</code> type should be declared, initialised and cleared:</p>
  <pre><code>{
    mpz_t integ;
    mpz_init (integ);
    …
    mpz_add (integ, …);
    …
    mpz_sub (integ, …);
  
    /* Unless the program is about to exit, do ... */
    mpz_clear (integ);
  }
  </code></pre>
  <p>Now, <code>mpz_t</code> is defined as follows:</p>
  <pre><code>typedef struct
  {
    int _mp_alloc;    /* Number of *limbs* allocated and pointed
                     to by the _mp_d field.  */
    int _mp_size;     /* abs(_mp_size) is the number of limbs the
                     last field points to.  If _mp_size is
                     negative this is a negative number.  */
    mp_limb_t *_mp_d; /* Pointer to the limbs.  */
  } __mpz_struct;
  
  typedef __mpz_struct *mpz_ptr;
  typedef __mpz_struct mpz_t[1];
  </code></pre>
  <p>and <code>mpz_init</code> is defined as:</p>
  <pre><code>void
  mpz_init (mpz_ptr x) __GMP_NOTHROW
  {
    static const mp_limb_t dummy_limb=0xc1a0;
    ALLOC (x) = 0;
    PTR (x) = (mp_ptr) &amp;dummy_limb;
    SIZ (x) = 0;
  }
  </code></pre>
  <p>Initially, I was confused as to why there are no <code>malloc</code>s in <code>mpz_init</code>. But after staring at the <code>typedef</code> for <code>mpz_t</code> for a while, my understanding is that the space for <code>mpz_t</code> is being stack-allocated, rather than heap-allocated (that is, it is an array of size 1, so it is effectively a stack-allocated pointer to the <code>__mpz_struct</code>). Having seen <a href="https://stackoverflow.com/questions/6390331/why-use-array-size-1-instead-of-pointer">this</a> question, the suggestion is that using an array of size 1 is an ISO C90 idiom, and the top answer of <a href="https://stackoverflow.com/questions/4523497/typedef-fixed-length-array/4523538">this</a> question seems to imply that we <em>shouldn't</em> go around doing this. Moreover, to me, this seems like a &quot;too-good-to-be-true&quot; way of getting out of manual memory allocation.</p>
  <p><strong>Questions</strong></p>
  <ol>
  <li>Why would you use this idiom? What benefits does it hold over <code>malloc</code>ing? What disadvantages does it have?</li>
  <li>Is it a historical thing? In C11/C23, would you do something else instead?</li>
  </ol>