Link blog: C, punning, programming, casting

Type punning isn’t funny: Using pointers to recast in C is bad.
A common C programming technique (casting between pointers to structures) leads to problems when strict aliasing is turned on (as it is if you set -O2 -O3 in gcc).
(tags: C programming casting punning)
Type Punning, Strict Aliasing, and Optimization – Embedded in Academia
More on the type punning/aliasing business.
(tags: C punning aliasing programming)

Link blog: funny, c, twiddling, programming

Bit Twiddling Hacks
A collection of code snippets for doing useful things (sign extension, determine whether a number is a power of two, and so on).
(tags: bit-twiddling programming algorithms c hacks twiddling)
Anti-Brexit traitors outed on twitter
UKIP voters in “shit thick” shocker. Also features Louise Mensch.
(tags: twitter ukip funny brexit satire)
Horse! | Start here: See horse, Say Horse! – The Rules of Horse!
Horse!
(tags: funny game horse)

Link blog: C, social-justice, computers, programming

The cost of forsaking C – Bradfield CS – Medium
Why teach C to undergrads.
(tags: C programming education computers)
The Unit of Caring — “I don’t think social justice is healthy.”…
A reasonable summary of the problems with Internet SJ stuff.
(tags: social-justice Internet culture Politics)

Link blog: development, C, testing, money

cmocka – unit testing framework for C
Nifty unit test framework which does the checking arguments and providing return values from stub/mocked functions which I tend to spend a bit of time re-creating each time I write a test.
(tags: test development programming testing unit-test C)
What cohabiting couples can to do put their financial house in order | Money | The Guardian
(tags: cohabitation finances will legal law money)

Link blog: programming, C, language, make

Implementing non-recursive make
Recursive makes are considered harmful. Here’s a recipe for a non-recursive one where you can still put project files in subdirectories.
(tags: make programming nonrecursive makefiles build software)
Embedded in Academia : Proposal for a Friendly Dialect of C
John Regehr and friends note that C compilers aggressive optimising around use of constructs the spec says are “undefined” can lead to unexpected behaviour. They propose a friendly C dialect where compilers would produce unspecified values in response to use of these constructs, but would not feel free to make demons fly out of your nose.
(tags: C programming language software-engineering)
The Left must reject the relativism at the heart of the Rotherham scandal | Left Foot Forward
This, from Al Razi of Ex-Muslim Forum, seems a sensible response, although as the worlds only impartial observer, I’d say that both the class of the victims and the race of both victims and perpetrators contributed to the horrors being ignored for so long. The Guardian will only talk about the former and the Telegraph about the latter, I suspect.
(tags: rotherham abuse religion islam news multiculturalism)

Link blog: funny, C, philosophy, shuffle

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)

Link blog: romania, mohammed, journalism, twitter

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)

Programming for a better tomorrow: the X-Macro

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.