c

Modern Microprocessors – A 90 Minute Guide!
Nice article about how modern microprocessors work.
(tags: microprocessor cpu hardware memory cache)
The Aural Majority: Do You Want to Swing, Virginia?
What makes music swing? With sound samples.
(tags: swing music shuffle rhythm)
Our quantum reality problem – Adrian Kent – Aeon
An article about quantum theory and the philosophy of science.
(tags: physics quantum research philosophy science everrett)
Printable True Bugs Wait Posters | natashenka
Abstain from strcpy! Wait for the string handling functions which are right for you.
(tags: programming funny security bugs C stdlib)

The Millions : One Fixed Point: “Sherlock,” Sherlock Holmes, and the British Imagination
Why we love Sherlock.
(tags: sherlock sherlock-holmes britain television books)
13 reasons why I am taking the Daily Mail to the Press Complaints Commission | British InfluenceBritish Influence
The Heil lied about Romanian immigration to the UK. This isn’t that surprising, but it’s nice to see someone do the research to prove it.
(tags: dailymail fail journalism lies uk romania immigration politics)
The Descent to C
Simon Tatham introduces C to people who’ve only worked in high level languages, the innocent little darlings. You ‘ad array bounds checking? You were lucky!
(tags: C programming language)
The Liberal Democrats face a true test of liberty | Nick Cohen | Comment is free | The Observer
“The real scandal in the Liberal Democrats is not leading the news. Extremists are menacing the career and life of a Liberal Democrat politician and respectable society hardly considers these authentically scandalous threats to be a scandal at all. The scandal, in short, is that there is no scandal.”
(tags: islam libdems liberal-democrats nick-clegg islamism freedom twitter mohammed)
The Millions : Read Me! Please!: Book Titles Rewritten to Get More Clicks
Classic literature titles re-written as those click-bait headlines you see spreading around Facebook: “They Told Him White Whales Were Impossible to Hunt. That’s When He Went Literally Crazy.” Via marn.
(tags: creative funny literature parody)

Here’s a C language technique which I’d not seen before starting my current job, which I think deserves to be better known.

Let’s say you have a bunch of things, to which you’ve given sequential integer IDs using an enum.

  enum my_things {
  thing_this,
  thing_that,
  thing_other,
  num_things
  };


For each of those things, you want to store some other information in various arrays, and index into the array using the enumerated value to pull that information out. Examples might be a parser (where the enums might identify operators, and an array might contain pointers to functions to implement them) or a message handling system (where the enums might identify types of message, and the array might contain pointers to functions to handle the messages).

void (*thing_fns[num_things])(void) = {
  do_this,
  do_that,
  do_other
  };
  
  void do_thing(enum my_things current_thing)
  {
  thing_fns[current_thing]();
  }
  
  void do_this(void)
  {
  /* wibble */
  }
  /* etc, etc. */
  


The problem comes when you want to add a new thing. You’re sizing the array using the enum, so it’s hard to get that wrong. But if you have a lot of things, sooner or later someone will add a new my_things member to the middle of the list (perhaps you’re listing your things in some order which makes sense in the context), go to update thing_fns, miscount how far down the definition thing_fns they’ve gone and screw up the ordering of the function pointers. Now the wrong function handles some (but not all) of the things, with hilarious results.

The solution is to maintain a single list of your things, and transform it into definitions for the enum and the associated arrays. You don’t need to write fancy code generator scripts for this, you can use the pre-processor. Wikipedia explains how.

(Note that Wikipedia’s example does away with sizing the array using the enum. They need to have a final NULL entry with no comma on the end to keep the compiler happy, so they’d end up writing the clunky num_things + 1. Now their array is sized correctly anyway.)

This chap goes into it in more detail, and also illustrates that your list of things doesn’t have to be in a seperate file.

Re-writing my example to use X-macros is left as an exercise for the reader. 🙂

Edited To Add: gjm11 points out that two of his friends have posted about this recently. gareth_rees has a more detailed example and some discussion.