title: How to initialize variable with cleanup attribute?
body: <p>Is there a way to initialize a variable with the <code>cleanup</code> compiler attribute? or do I have to set the value after declaring the variable?</p>
  
  <p>I've tried putting the <code>cleanup</code> attribute in front of <code>= malloc(10);</code> like in the example below and behind <code>= malloc(10);</code> but neither compiles. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  static inline void cleanup_buf(char **buf)
  {
      if(*buf == NULL)
      {
          return;
      }
  
      printf("Cleaning up\n");
  
      free(*buf);
  }
  
  #define auto_clean __attribute__((cleanup (cleanup_buf)));
  
  int main(void)
  {
      char *buf auto_clean = malloc(10);
      if(buf == NULL)
      {
          printf("malloc\n");
          return -1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Is there a different syntax for using <code>cleanup</code> and initializing the variable on one line? Or do I have to set the value after declaring the variable like in the example below?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  static inline void cleanup_buf(char **buf)
  {
      if(*buf == NULL)
      {
          return;
      }
  
      printf("Cleaning up\n");
  
      free(*buf);
  }
  
  /* Use this attribute for variables that we want to automatically cleanup. */
  #define auto_clean __attribute__((cleanup (cleanup_buf)));
  
  int main(void)
  {
      char *buf auto_clean;
  
      buf = malloc(10);
      if(buf == NULL)
      {
          printf("malloc\n");
          return -1;
      }
  
      return 0;
  }
  </code></pre>