The sizeof operator in C tells us how much space the result of an expression would use if we were to evaluate it, but doesn’t actually evaluate the expression. For example, sizeof i++ yields the size of the variable i , but doesn’t increment i . Many implementations of C provide a typeof operator too, which provides the type of an expression without evaluating it. That lack of…
I’ve been interviewing job candidates lately, so I’ve been thinking about interview questions. I always ask people to write code in a technical interview, and I usually start with a fairly easy warm-up question — some sort of a toy problem like an operation on lists or trees, often the kind of thing that has a neat recursive solution. There’s a particular sinking feeling that comes when the…
So you have a multithreaded program, and you need to wait for something to happen: a server to respond, a resource to become available, that sort of thing. But you don’t want to wait forever – if the server hasn’t responded in a few seconds we’d like to try another one, or report an error. The POSIX threads API for this is pthread_cond_timedwait() . It blocks the calling thread,…
Playing around with bootloaders got me thinking about CRCs again. In particular, about using them for purposes other than their intended one. CRCs were designed to detect accidental corruption in electronics, and they’re pretty good at that – single-bit errors in the input change multiple bits in the output and two single-bit errors rarely cancel each other out. But CRCs are not…
Today, a cautionary tale about clever compiler tricks. I was debugging a tricky test case and wanted to be sure the compiler wouldn’t optimize away any of the important state, so I disabled optimizations in the part of the code I was interested in. Simplified, it looked like this: #include <string.h> char buf[1024]; #pragma GCC push_options #pragma GCC optimize "-O0" void function (void) {…
Last time I got as far as a U-Boot prompt on the LinkStation Live, ready to TFTP-boot FreeBSD on it. Cross-building FreeBSD is absolutely delightful, if you’re used to Linux: export MAKEOBJDIRPREFIX="${HOME}/FreeBSD/obj" export DESTDIR="${HOME}/FreeBSD/dest" export TARGET=arm export KERNCONF=HS-DHGL make -j4 buildworld buildkernel (as root) make installworld distrib-dirs distribution but…
I need a small server to hold backups and do one or two other odd jobs on my home network, so I bought a cheap (£40) NAS box from ebay, to see if I could get FreeBSD running on it. I chose a Buffalo LinkStation Pro, which uses the Marvell 88F5182 “Orion” SoC (based on ARM’s ARM926EJ-S core, and pretty widely supported), with U-Boot-based firmware. When it arrived, it turned out…
Some of the library functions in the C standard (and a lot more in POSIX) set errno to report what went wrong. The interface is pretty simple: the return value of the function tells you that there’s been an error, and then errno tells you what it was. And since it’s a standard interface and people are used to it, you may be tempted to use it in your own library APIs. After all,…