News for hackers. Stuff that matters.
Re:C2x: the next real revision of the C standard by v_lisivka on Monday November 12, 2018 @11:53AM (#18433736)

  /**
   * GCC and Clang extension to call cleanup function at exit
   * from the function with pointer to given function as argument.
   */
  #define defer(func) __attribute__((cleanup(func)))

  #ifdef _CRUST_TESTS
  
  // Sample destructor, to test defer
  static void _tst_charp_destroy(char * * value) {
    if(*value) {
      free(*value);
      *value = NULL;
    }
  }
  
  // Test case
  it(defer, "must deallocate object at the end of the function") {
    defer(_tst_charp_destroy) char * s = calloc(1, sizeof(char));
    (void)s;
  }
  
  #endif

There are no comments on this comment yet.