C Collections

cstring · Section 3

cstr_construct_scoped

cstr_construct_scoped - declare and initialise a cstr with automatic RAII

C Collections (libccollections, -lccollections)

#include <ccollections/cstring.h>
 
cstr_construct_scoped(s, initial);

This macro does cstr_declare_scoped() and cstr_init() in one statement,
and the string is destroyed automatically when the variable leaves its
scope.

s        Variable name to create
initial  Initial content (C string or NULL for empty string)

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

{
  cstr_construct_scoped(s, "auto-freed");
  /* use s ... */
}  /* the program destroys s here automatically */

cstr_construct(3), cstr_construct_mp_scoped(3)