December 2007

You are currently browsing the monthly archive for December 2007.

Christmas 2007

Another Christmas adventure. This year I spent the holiday with my family up in Killingon, VT. My brother put in a movie today called Ski Patrol. It just so happens that this movie, released in 1990 was my absolute favorite movie as kid. I must have seen it about a thousand times. Yet, I hadn’t seen it in nearly 5 years.

You know what I learned? 5 years wasn’t long enough, I still knew every word :-) It’s okay though, I had a very fun time watching, and it was great to spend some time with the family. Hope everyone else had a great holiday as well.

I was in a job interview the other day and someone asked me the following question, which I got wrong. Its not hard to remember, but I figure if I pass it on, and anyone out there who reads this blog for technical content might get a little refresher.The question was something like: write the declaration for an array of function pointers that take an integer and return a double in C.

This problem isn’t overly complicated, but C can be a tricky language. I used the language for a year of solid development, and never had to use anything this complicated.

My attempt at this problem without any resource ended up looking like:
double function_array(int foo)[];

Okay, before you laugh, remember I haven’t had to ever actually defined a complicated structure like this. What this translates to in C is: a function named “function_array” is takes an int “foo” and returns an array of doubles. This is not possible in C as you can’t return arrays. You can return pointers, which can be indexed as an array, but you cannot return an array.

The solution is actually:double (*function_array[])(int foo);which solves the problem as stated.

The resource that I used to find the answer is a book called Expert C Programming: Deep C Secrets. This book is AMAZING and you should definitely invest in it if you do any serious work with C. It does have a bit of a Sun systems bias, as the author was a compiler writer for Sun Microsystems, but the book has useful information for anyone using the language. There is a whole chapter on unscrambling declarations in C.

So there it is, another friendly reminder about declarations in C.

Update:

After rereading the post I realized that I was not content with my understanding of how to use arrays of functions. I came up with a mock project to work on. Lets say you have an input string, and would like to process it to remove some special characters (in my case I used “:”, ” “, and “\t”). I wrote a function for each special character and iterated through all the functions on each string. This might not be the most expedient way of solving this problem, but it allowed to me to test the use of arrays of functions.

You can see the source: here. It has some brief inline documentation, but I’m not doing anything else too crazy. One additional note, I rediscovered that if you initialize a string like char *foo = "some string"; it stores it read only memory. Note that is different than char foo[] = "some string" which is stored in read/write memory.

In a recent article at Coding Horror, the author discusses Hash Tables. This is a very strong post on hash tables and their hashing function. It is definitely worth the read, but there is one thing I want to comment on.

In the post Jeff Atwood discusses hash tables and states the following:

Key-value pairs are quite common in real world data, and hashtables are both reasonably efficient in storage and quite fast at lookups, offering O(1) performance in most cases. That’s why hashtables are the go-to data structure for many programmers. It may not be the optimal choice, but unlike so many things in computer science, it’s rarely a bad choice.

and while I don’t disagree with this comment, I’m afraid that some programmers might misunderstand what is being said here. This post mostly covers the concept of hashing functions. This page is NOT saying that hash tables ARE the solution to EVERY problem.

One of the biggest problems I see in code that I look at is that people are not fully thinking out their data structures before writing code. Linked lists are great if you are going to be adding elements constantly and searching through your items rarely, but if search is of high demand, that maybe you would want to consider something that has less of a search time. I’m not going to go into Big O Notation here, but that is another worthwhile concept for any computer programmer to understand.

Hash tables are great for searching one-to-one mapped pairs. If you are keeping track of person to phone number, than a hash table would be an accurate representation, and a good tool to solve the problem. If the problem were to get more complicated, lets say we start holding the phone number in an object designed to store more than just phone information. If we are writing an App that only looks up by person, then hash table might still be the best choice. If we change our search parameters, perhaps we start looking for people in a certain town, or we have a phone number and want to see if it is associated with anyone in our records, this becomes a costly search.

We end up having to look through all records, because the hash is keyed off of data that is irrelevant to our search. This problem can be solved in many different ways, and the solutions are beyond the scope of this article. My one point is that while hashing is very unique and powerful, it is the the universal tool that solves any and all problems. A very important precursor to Jeff’s article is to decide whether a hash table is indeed the solution you seek, otherwise, space and hashing functions may be the least of your problems.

Villanova LogoHello all you Villanova fans out there. If you are like me, and the further you get from graduation the less you remember about your old college basketball team, then this link is for you. Okay wait, what is this? It is a link to an iCal formatted subscription. iCal is also the name of Apple’s calendar app that ships with Mac OS X.

Vacation Trip Preview

California FriendsI promise to write more on my vacation later, but I wanted to give a little preview. Here is a photo of my California friends. Just wanted to point out that three of us are relatively serious photographers. Why is the Sun behind us?