C kata

Reader, forgive me for going all Fred Dagg on you, but sometimes we don’t know how lucky we are.

Last month I was working on one of the anagram problems in chapter two of Jon Bentley’s Programming Pearls. I was writing code in C and having trouble with something that seemed pretty basic, something that took me all of a minute in Ruby.

All I wanted to do was write a function in that took a string as an argument and returned a new string containing all the characters of the original string sorted in alphabetical order. Getting it (kind-of) right in C took several hours of faffing around.

I came away with a renewed appreciation of Ruby and a great deal of respect for people who get their hands dirty with C & C++ every day.

I’ll spare you the timeline of my travails and point you to my code and talk about what I learned in my kata.

Strings aren’t really strings

When I said I wanted to write a function that took a string as an argument, I meant an array of the char type. Unlike Ruby or Java, there’s no specific string type with a bunch of handy built-in functionality. The standard library has a bunch of string handling functions but it’s not really the same thing.

You have to think about the memory

My function didn’t return a new string. It didn’t return a new array of char. It created a new array of char and returned a pointer to the address in memory containing the first element of the array. Arguments and return types are nearly always pointers.

When I created the new string I had to allocate sufficient memory to represent all the characters in alphabetical order. And I had to remember to free the memory when I no longer needed it.

In another part of my program I wanted an array of strings — an array of arrays of char. How much memory to allocate? How long is a piece of string? When to free it? How soon is now?

C is a simple programming language and you can do great things with it

There’s really not that much to learn with C. Arrays, pointers, structs, data types, standard library. That’s pretty much it. You’re on your own after that, and that’s fun, frustrating, and dangerous. I encountered garbage output, segmentation faults, and a memory leak. I have no idea if what I wrote was insecure, I suspect it was. I think C is a little bit like Voldemort, terrible but great.

Ruby is fucking great

For all the things that I do, ruby is a friendly and powerful tool. It’s fun to write, it takes care of all the things I don’t want to care about. I’m lucky to be able to use it.