Archive for March, 2010

Bye Bye mall##oc

No Comments »

Using malloc() and free() necessitates care; a lot of care. In fact, I can not remember seeing any non trivial C program, that would not have an extra memory management layer. Often a double linked list was involved and atexit() reporting of non free()ed memory. At the very least some simple reporting was going on usually redefining malloc() in terms of mall##oc(). I recently used the latter construct, to show off my superb arcane knowledge of the C pre processor, when I was met with skepticism; What was wrong with the LOC below?
redefinemalloc
To demonstrate (how little this PFY knew and) how cpp was going to balk at the recursive definition, I typed it in and, low and behold, the compiler accepted it and generated the right code. All I could do was grin. Of cause I had always realized that not adding a token to the symbol table before the whole definition was parsed would be better, but compilers just never did. Who changed this when? And would this open the door to recursive macro’s, does the pre processor do a multipass? So I ran the two snippets below through gcc -E to expand A(4).

Fail Redef
A(4)A(4 +1 +3) A(4)(4 +1 +3)

As you can see, there is some lazy expantion going on. If we RTFM, it shows that: Each macro is expanded when it appears in the definition of the other macro, but not when it indirectly appears in its own definition. Ok, I’ve learned never to trust things to stay the same, again.


Clock Arithmetic Still Hard

3 Comments »

Clock or modular arithmetic is hard. Take a look at the two code snippets below. Both seem reasonable integer comparison functions modeled after strcmp(). However, the left one contains an overflow bug.

BuggyIntCmp ClassicIntCmp

I have thirty odd years of programming experience, yet I had to be pointed out the bug. The left snippet looks obviously wrong to me, now. It’s like claiming that we go back in time if the short hand of a clock moves more than 6 hours. Ridiculous. (And, no, intermediate casting to long won’t help.) Just try a few extreme cases using INT_MIN, for example. Clock arithmetic is hard because if the modulus is sufficiently large we tend to ignore it. SimplifiedIntCmp So, if you have to go against the first directive, if you don’t trust the -O4 option, or if you just have to show off how brilliant you are, use something like the C code on the right. It won’t bring you much, but at least it does not contain an overflow bug. Though it might contain an other one, or show a incompatibility in your compiler. But that is left as an exercise to the reader.


Random Is Not Fair

No Comments »
Picture 1 Picture 2

Randomness is really not compatible with the human mind. We see order in everything. Only homogeneous distributions feel random to humans. If you role a dice three times and it comes up sixes, you (and me) either suspect the dice is weighted or the next throw, for sure, is going to be not-six. Don’t deny it, random must be “fair.” Look at the dots in the scatter plots above, which is schemed and which is random? Sometimes a whole paper is needed to convince scientist that distributions are just random and that there is no underlaying scheme. Look for example at [Clustering of the Cosmic Ray Ages of Stone Meteorites] (Andrew S. Tanenbaum).
To accommodate human feelings some form of control over the randomness is needed. “The way to handle controlled randomness is actually pretty simple. It’s commonly called a shuffle bag.” (Sean McArthur) [A Less-Random Generator] Almost all successful games use random control like that.


La Coder Qui Rit

No Comments »

LVQRHumor comes in many guises. Coding humor is no exception. Next to jokes in comments like:

  // Message for the dyslexic:
  // There is a Dog!

humor can be be code specific like this LOC from a message server:

  for (-8  ;-(  8)  ;o)

or, alternatively:

  while (THE_POPE_IS_CATHOLIC)

Some go overboard (as coders often do) and change the language all together:

likepython latinperl
Like, Python Lingua::Romana::Perligata

To me, it becomes interesting when I have to wonder: “Is it a joke, or not?” Look at the code below and tell me, given that this is part of the MINIX POSIX compliance test set, is this juste pour rire?

LCQR