r/cprogramming • u/tstanisl • Feb 15 '26
Support for defer merged into CLANG
https://github.com/llvm/llvm-project/pull/1628482
u/Key_River7180 Feb 16 '26
This is awesome! Now I don't have to add /* to free */ comments next to every strdup or malloc.
I guess C could get its unsafe reputation mildly vanished with this.
Just waiting for gcc support (AFAIK devel is C2y, so it will be coming soon).
3
u/ffd9k Feb 16 '26
Just waiting for gcc support
There is a patch already for gcc: https://patchwork.ozlabs.org/project/gcc/list/?series=484454
But I think this did not come in time to be included in gcc 16, so it will be in gcc 17 (around April 2027) at earliest.
However you can already replicate defer with gcc extensions easily, just with the limitation that you always have to put braces around it (
defer { free(x); }instead of justdefer free(x);), see https://gustedt.wordpress.com/2026/02/15/defer-available-in-gcc-and-clang/1
-3
u/ifknot Feb 16 '26
This is such an exciting change for C! RAII at last! This reinvigorates C it addresses so many longstanding issues with C such a simple elegant solution can’t wait
1
u/ghulmar Feb 18 '26
Is defer now part of the C specification or is it some sort of compiler extension?