Home > Articles > Programming > C/C++

  • Print
  • + Share This
From the author of Concurrency

Quick Exit

One of the features with a less obvious rationale is quick_exit(). This is a third way of terminating a running program, intended to be somewhere between abort() and exit(). This addition was motivated by the sort of problem that caused a vulnerability in OpenSSH a few years ago: Sometimes you really need to do cleanup before exiting, but if your program is already in an undefined state, it's dangerous to run all of your cleanup code.

With at_quick_exit(), you can register a function that will be run when quick_exit() is called. This function should do very simple cleanup, but also should be careful not to visit any complex data structures that may be corrupted.

Unicode

One of the most welcome additions in C11 is the ability to put Unicode string literals into source code. They have similar syntax to the existing wide character literals. For example, u8"UTF-8" gives a UTF-8 string literal (which, in this specific case, is also an ASCII string literal). The other options are slightly more complicated: In general, you can hope that u"UTF-16" is UTF-16 and U"UTF-32" is UTF-32. If this is the case, then the compiler should define __STDC_UTF_16__ and __STDC_UTF_32__ as macros that evaluate to 1. If these macros aren't set, strings prefixed with u and U can still be parsed, and they should expand to some 16- or 32-bit encoding, but the encoding used is not specified.

The new uchar.h header contains utilities for converting between different Unicode and wide character encodings. I've never been a fan of the wide character support in C; the parts defined by the standard library are just sufficiently complex to be difficult to implement, but not complex enough to be useful. Having some basic support for Unicode strings is quite useful, however.

Compiler Support

Despite taking 12 years to write, the new C specification doesn't actually add very much beyond C99. That's not a criticism; C99 is a solid low-level language that needed little improvement. Most of the new additions are either relatively simple or already supported (often with different syntax) as compiler extensions, so you should see C11 supported by several compilers in 2012.

It's also possible to support a lot of the new features without modifying the compiler. For example, the FreeBSD implementation of <stdatomic.h> also includes an implementation that uses the GCC __sync_* family of built-ins to provide the operations. This version is (potentially) slower, but works well with GCC 4.2. You can implement _Static_assert() by using a little hack that declares an array of 1 or -1 elements, depending on a condition. Declaring an element of -1 elements is invalid, so the compiler will reject the source file.

As with C99, lots of obscure parts of the C11 specification will require a more complex implementation; because these parts are less interesting to developers, they may take a long time to be implemented. I'm not sure whether any 100%-compliant implementations of C99 exist yet, but most C compilers have long implemented the subset that people actually want.

  • + Share This
  • 🔖 Save To Your Account