Modern C for C++ peeps (2019)
You can have RAII with GCC extensions in C:<p>char *buf __attribute__ ((__cleanup__(free_buf))) = malloc(1024);<p>This calls free_buf(buf) when buf goes out of scope.<p>I’ve actually seen projects use it in the wild to close files or free memory in branchy functions. [1]<p>[1] <a href="https://github.com/containers/bubblewrap/blob/c54bbc6d7b78e7a45016efe0c4ac8af9d209aa20/utils.h#L165">https://github.com/containers/bubblewrap/blob/c54bbc6d7b78e7...</a>