Category Archives: Bugs, mean bugs, and C

Horrible specificities of the C language which may cause hard to discover bugs. A recommended read if you encounter bugs that you cannot identify.

gettimeofday() and ctx->cur_loop_time

The event manager keeps track of the current time in the cur_loop_time field of the Orchids context. When you want to reschedule an event to take place in, say, 5 seconds, it is tempting to write:

  heap_entry_t *he;

  /* ... */
  he->date = ctx->cur_loop_time + 5;
  register_rtaction(ctx, he);

This post explains why this is a bad idea, and how it should be done. Continue reading

Type-punning and the strict aliasing rule

The ANSI C standards were obviously largely influenced by compiler writers. This is a good thing: these are the guys would should know about the language. However, they sometimes enforce semantics assumptions that allow for very efficient optimizations. Ultimately, the main problem is that these assumptions are not or only partially checked by the compiler, but the compiler will still do as though these assumptions are true, and modify your code accordingly. I will concentrate on one of these assumptions here: the strict aliasing rule, and its pretty bad way of mixing with type-punning. Continue reading