title: Multiple structures in a single malloc invoking undefined behaviour?
body: <p>From <a href="https://wiki.sei.cmu.edu/confluence/display/c/DCL38-C.+Use+the+correct+syntax+when+declaring+a+flexible+array+member" rel="nofollow noreferrer">Use the correct syntax when declaring a flexible array member</a> it says that when <code>malloc</code> is used for a header and flexible data when <code>data[1]</code> is hacked into the <code>struct</code>,</p>
  
  <blockquote>
    <p>This example has undefined behavior when accessing any element other
    than the first element of the data array. (See the C Standard, 6.5.6.)
    Consequently, the compiler can generate code that does not return the
    expected value when accessing the second element of data.</p>
  </blockquote>
  
  <p>I looked up the C Standard 6.5.6, and could not see how this would produce undefined behaviour. I've used a pattern that I'm comfortable with, where the header is implicitly followed by data, using the same sort of <code>malloc</code>,</p>
  
  <pre><code>#include &lt;stdlib.h&gt; /* EXIT malloc free */
  #include &lt;stdio.h&gt;  /* printf */
  #include &lt;string.h&gt; /* strlen memcpy */
  
  struct Array {
      size_t length;
      char *array;
  }; /* +(length + 1) char */
  
  static struct Array *Array(const char *const str) {
      struct Array *a;
      size_t length;
      length = strlen(str);
      if(!(a = malloc(sizeof *a + length + 1))) return 0;
      a-&gt;length = length;
      a-&gt;array = (char *)(a + 1); /* UB? */
      memcpy(a-&gt;array, str, length + 1);
      return a;
  }
  
  /* Take a char off the end just so that it's useful. */
  static void Array_to_string(const struct Array *const a, char (*const s)[12]) {
      const int n = a-&gt;length ? a-&gt;length &gt; 9 ? 9 : (int)a-&gt;length - 1 : 0;
      sprintf(*s, "&lt;%.*s&gt;", n, a-&gt;array);
  }
  
  int main(void) {
      struct Array *a = 0, *b = 0;
      int is_done = 0;
      do { /* Try. */
          char s[12], t[12];
          if(!(a = Array("Foo!")) || !(b = Array("To be or not to be."))) break;
          Array_to_string(a, &amp;s);
          Array_to_string(b, &amp;t);
          printf("%s %s\n", s, t);
          is_done = 1;
      } while(0); if(!is_done) {
          perror(":(");
      } {
          free(a);
          free(b);
      }
      return is_done ? EXIT_SUCCESS : EXIT_FAILURE;
  }
  </code></pre>
  
  <p>Prints,</p>
  
  <pre><code>&lt;Foo&gt; &lt;To be or &gt;
  </code></pre>
  
  <p>The compliant solution uses <code>C99</code> flexible array members. The page also says,</p>
  
  <blockquote>
    <p>Failing to use the correct syntax when declaring a flexible array
    member can result in undefined behavior, although the incorrect syntax
    will work on most implementations.</p>
  </blockquote>
  
  <p>Technically, does this <code>C90</code> code produce undefined behaviour, too? And if not, what is the difference? (Or the Carnegie Mellon Wiki is incorrect?) What is the factor on the implementations this will not work on?</p>