this article is meant to address the constant C-nile accusations i'v gotten for using C as my primary languge. Which is
is little annoying , so i've decide to talk about it here.
C is known as an unsafe language due to it's lack of built-in safety features such as bounds checking, type safety and automatic resource management,
and has the
famous saying. "it is easy to shoot yourself in the foot"
Well since most of C's unsafety relates to memory issues and UB. what if i told you that , you could literally just write safe C code and it is simple.
"You can just do things" they said. C is as unsafe as any other langauge because 100% safety doesn't really exist. "even rust?", yes even Rust.
This is not a pro-C evengalism article, so i won't lie. achieving saftey in C is a bit tricky, especially when working with teams and on really large code bases,
but that doesn't cancel out the fact that it is entirely possible to do so. so here are tips on how i write safe C.
One way i improve safety in my C programs is relying on static allocations, I don't even remeber the last time i dynamically allocated memory for anything.
With static allocations, data strcutres and variables are fixed sized, therefore determined at compile time and researved in global or stack memory. The lifetimes of such
are fixed , they exist for either the duration of the program (static/global variables) or the function(stack variables)
This way is convinient because variables have predictable lifetimes and i get to avoid any pontential dynamical allocation issues such as memory leaks, double frees or use-after-bugs ,etc.
"You can just not do things."
Also, since all storage is pre-allocated, there's no chance of heap fragmentation or out-of-memory errors during runtime.