Giving C a Superpower: custom header file (safe_c.h)

24 points by linkdd


hoistbypetard

Perhaps this is a failure of imagination, but it read to me like a corollary to Greenspun's 10th Rule. It'd be easier and significantly less error-prone to use a C++ compiler and standard library, and ban whatever C++ features you don't like by convention.

lor_louis

I can't argue with the results. If it works for you and you are happy with the resulting code, I can see this being a significant improvement over "normal" c.

Personally, I've tried RAII in C with cleanup attributes and just found it too clunky. Maybe with C2Y's defer proposal this kind of programming might become viable for me. In the meantime, I highly recommend giving custom userspace allocators a shot. Having a way to free all of the memory at once really simplified the way I program.

https://thephd.dev/c2y-the-defer-technical-specification-its-time-go-go-go

https://www.rfleury.com/p/untangling-lifetimes-the-arena-allocator

FRIGN

The idea behind safe_c.h is good, but it's a matter of taste if you want to use that many macros, though. You end up with a metalanguage that is very hard to read for someone not familiar with the codebase. The article inspired me to look further into RAII mechanics in C23.