C Collections

cstring · Section 3

cstr_declare_scoped

cstr_declare_scoped - declare a cstr variable with automatic RAII destruction

C Collections (libccollections, -lccollections)

#include <ccollections/cstring.h>
 
cstr_declare_scoped(s);

This macro works like cstr_declare() but also attaches a cleanup
attribute, so the string is destroyed automatically when the variable
leaves its scope. The cleanup does exactly what cstring_destroy() does:
it frees the internal buffer and the container and sets the variable to
NULL. The macro initialises the variable to NULL, so you can leave it
unassigned for the whole scope and the cleanup then does nothing.

s   Name of the variable to declare

This macro needs GCC or Clang, because it uses __attribute__((cleanup))

{
  cstr_declare_scoped(s);
  cstr_init(s, "auto-freed");
  /* the program destroys s automatically at the end of this block */
}

cstr_declare(3), cstr_construct_scoped(3), cstring_destroy(3)