irrelevant


Modern C

There are a lot of discussions on the interwebs about Modern C, so I'll pollute the environment with my own take on this.

At work, a colleague of mine have been giving training on Rust. Now, I am going to make it clear from the beginning that I am not a fan of Rust. However, I found the first couple of lectures extremely interesting as we went through the type system, the core concepts of the borrow model etc. But. As we got to actually do stuff in Rust, I just couldn't be bothered. I find the syntax obnoxious and I feel like I'm just sprinkling Box'es and &'s around until it compiles. This gives me the feeling of being back in school, trying to understand pointers in C and just keep taking addresses or dereferencing until the segfault went away. My point here, is that I just don't get Rust yet. I'm not going to ignore Rust, I'm just not ready to embrace it yet.

Well, back to C. Should be clear from by blog that I enjoy doing C. I try very hard to do what, in my mind, is "modern C". That is, I'm a big user of compiler features to help me and my own support libraries.

My "safe" allocation routines are a must. See my updated post on this. I use non-standard stuff for strings, like asprintf(). I rely heavily on autofree. I use a set of compiler options to help me as well.

Here, example for meson.

c_args: [
  # add some useful warnings
  '-Wconversion', '-Wdouble-promotion', '-Wundef', '-Wno-trigraphs',

  # warn about c99 declaration after statement
  '-Wdeclaration-after-statement',

  # variable-length arrays are a source of bugs
  '-Wvla',

  # sign conversions are useful and generally not a source of bugs
  '-Wno-sign-conversion',

  # don't do bad things on integer overflow
  '-fno-strict-overflow',

  # add -g3 for debug (more symbol information)
  get_option('buildtype') == 'debug' ? ['-g3'] : [],
],

I use a lot of helpers like checkpatch and sparse and I try to write clean code.

I also rely on good clean support libraries from third-parties. Specifically, CCAN.