cstring · Section 3
cstr_declare_scoped
NAME
cstr_declare_scoped - declare a cstr variable with automatic RAII destruction
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cstring.h> cstr_declare_scoped(s);
DESCRIPTION
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.
PARAMETERS
s Name of the variable to declare
NOTES
This macro needs GCC or Clang, because it uses __attribute__((cleanup))
EXAMPLES
{
cstr_declare_scoped(s);
cstr_init(s, "auto-freed");
/* the program destroys s automatically at the end of this block */
}
SEE ALSO
cstr_declare(3), cstr_construct_scoped(3), cstring_destroy(3)