Yearly Archives: 2012

C code that could get you fired

Here’s another stupid C trick I’ve carried around in my brain for a while:

#define sizeof(x) (rand() % 20 + 1)

The mod and addition are to give a random number in the range from 1 to 21.

This can be demonstrated with a small program:

#include <stdlib.h>
#include <stdio.h>

#define sizeof(x) (rand() % 20) + 1

void main()
{
int i;
printf("Sizeof i: %d\n", sizeof(i));
printf("Sizeof i: %d\n", sizeof(i));
printf("Sizeof i: %d\n", sizeof(i));
return;
}

When run, you get:

# ./random_sizeof
Sizeof i: 4
Sizeof i: 7
Sizeof i: 18

Your compiler might warn you about this, although mine didn’t (using GCC 4.3.2 with no flags) – but on a large project, there are probably so many warnings that an extra one wouldn’t be noticed anyhow (if all the large projects you’ve worked on had no warnings at all, you’re luckier than most of us).

Unlike the trick I posted previously, using this in your code will not have your colleagues worry about your competence so much as it will prompt them to fill a box with your things and show you the door…

Filed under code

Wherein 2 is shown to be equal to 1

Here’s a little maths trick I have for some reason carried around in my brain for years:

$\text{Let } a = b$
$\Rightarrow a^2 = ab$
$\Rightarrow a^2 - b^2 = ab - b^2$
$\Rightarrow (a + b)(a - b) = b(a-b)$
Dividing through by the common $(a - b)$
$\Rightarrow (a + b) = b$
$\text{But } a = b$
$\Rightarrow (b + b) = b$
$\Rightarrow 2b = b$
$\Rightarrow 2 = 1$

Why this is possible is left as an excercise for the reader 🙂

Filed under geekery

C code you should never use in production

I’ve been meaning to write this little trick down for a while, so while I remember to, here’s a stupid little C trick which amuses me.

Consider a C array:

char array[3] = {1,2,3};

To access element 3, you would use

array[2]

This is just shorthand for a pointer dereference

*(array + 2)

This is just addition, which is associative, so it’s the exact equivalent of

*(2 + array)

which gives you

2[array]

which is perfectly valid C.

To prove that, consider this simple program

void main()

{

char array[3] = {1,2,3};

printf("array[2]: %d. 2[array]: %d\n", array[2], 2[array]);
}

After compilation

./assoc.out
array[2]: 3. 2[array]: 3

I would strongly caution against ever using this in production. Your colleagues will worry about you if they see it during a code review or when working on code you wrote. If you don’t think they would flag it, you should be worrying about them 🙂

Filed under code

Three years down the line

Three years ago today, I made a commitment to the Lord and the world that I wanted to spend the rest of my life with an amazing woman who had become my everything.

Three years down the line, I’m even more blissfully happy than I was then – thank you Vicki for giving me a terrific year with you, and here’s to many more happy years together. Happy anniversary love.

Filed under Uncategorized

Headphone jacks do not belong on the front of laptops

Attention laptop designers: Don’t put the damned headphone jack on the front of the laptop, for crying out loud! You’ve forced me to choose between annoying background noises or annoyingly having to rest my arms on the headphone cable and constantly bumping the jack. Thanks ever so much.

HP is the guilty party here, but they’re not the only ones.

Okay, rant over, normal service can resume.

Filed under computers

Sumatra PDF

I’ve been using Pandoc for generating most of my documents for some time now, even going so far as to write a small tool to add additional support specific to my needs. I mostly generate PDF documents, and I found it quite annoying to have to close the PDF viewer (I was using Acrobat) to allow Pandoc to make a new PDF.

I have since discovered that the Sumatra PDF reader will allow PDF’s it has open to be written to – I’ve been using it for a few days and it seems a nice solution to this little problem.

Filed under computing

Deluge Toggle – the world’s simplest Deluge client

I’ve recently been using Deluge to download torrents, thanks to froztbyte’s recent blog post. It seems to work very well, especially when combined with FlexGet for handling RSS feeds of torrents.

My torrents are downloaded by my desktop machine, which shares bandwidth with the rest of the devices on my network – my phone and my laptop, generally. I prefer the torrents not to use any bandwidth while I’m home, to free it all for whatever else I want it for.

Deluge offers scheduling, which is fine for weekdays – on almost any weekday the torrents can happily download during working hours and sleepy time. However, our weekends can vary wildly between home all weekend and barely home at all – it seems wasteful to schedule no downloading for the non-sleepy hours of the weekend on the off chance we’ll be home.

The Toggle plugin offers a better solution for my use-case – it stops all Deluge bandwidth usage when toggled. It integrates into the Deluge GUI and web clients.

However, I have found the web client to be quite slow and clunky on my phone, which is the primary device I want to use to toggle Deluge’s bandwidth usage – generally when I wake in the morning and go to bed at night. This is quite possibly my phone at fault, but it does make toggling Deluge’s bandwidth usage annoying, since I often need to use my laptop or the desktop machine.

Accordingly, I have written what I would imagine is the world’s simplest Deluge client: deluge-toggle. The client either reports what the current toggling status is, or toggles the bandwidth usage. That’s all it does, but that’s exactly what I needed.

I have also written an incredibly simple Flask webapp to interact with the deluge-toggle client, to save me having to use a console from my phone. I run this webapp on my desktop machine, and can now easily toggle Deluge’s bandwidth usage from anywhere that I can connect to my home network.

All of this is very simple, but it does say complimentary things about Flask and Deluge (particularly its plugin system) that this was as simple as it turned out to be.