Thursday, January 29, 2009

Project 2: String Length Counter in C

This weekend I spent some time exploring C strings, and comparing/contrasting them with strings in C++ after doing a project in which we were required to write a word length counter without using the built-in stringlen functions (oh noes!).

Strings in C++ are nothing like strings in C. I had a vague idea of this when I was doing the project, as I have used C++ strings in the past, and they don't require as much work as C strings. There is a very good reason for this: while C++ strings are very nice containers with nice built-in functions, C strings are, quite literally, nothing but a group of chars. This can be expressed either as an array or a pointer. The difference between the two being one is a pre-sized block of memory containing a line of chars, while the other is an address to a block of memory (not necessarily pre-sized) containing a line of chars. Array do decay into pointers when being passed as parameters and such, but the two are still fundamentally different.

A very interesting thing about pointers and arrays is that, despite the fact they are represented completely different in memory, they can still be treated the same within this context. If you declare two variables:

char array[] = "Kitty";
char *pointer = "Cat";

you can use the same notation to access the individual characters in both, ie:

array[1] to get i
pointer[1] to get a

In the first case, the compiler will start at the first character of array and move one in order to get the value. In the second case, the compiler will fetch the pointer value, add 1 to this value, and then finally go to this location to load the character.

This is what, for me, makes C so interesting. It is far more low-level than C++, and as such, the fact that you're accessing values in memory is far more transparent. The malloc command I used in my homework literally sets aside a block in memory of the size indicated (returning a pointer), and keeps that memory allocated until it is either deallocted by free or until the program ends.

C++ string containers are just special templates that allow you to do far less damage accidentally (though it is said you can pretty epically destroy the world if you do mess up). They do a lot more to automatically manage your memory for you. You can convert them into a c_str (which is actually a const *char, and is necessary for a few file input functions such as fstream), but for all intents and purposes they are their very own, very easy to use data structure.

Anyway, I hope to do more C programming this year as a way to improve my knowledge of pointers, and hopefully allow me to start writing some more heavy-duty projects such as small compilers and what-have-you. Let the C adventures begin!

Thursday, January 22, 2009

Project of the Week

I decided that since I've stopped posting regularly (unfortunately!) I'm going to start post about a Project of the Week. Essentially, this will be some project that I accomplish, either over the entire week or just on a weekend. Posting my experiences will both help me remember it, and provide a reference for myself. And maybe someone else will enjoy reading about it as well :)

This week's was easy: set up my Ubuntu server with SSH and an IP address manager daemon that would allow me to admin the server remotely, and elimiate the need (for the most part) for the splitter box I have between my Mint desktop and the server itself. This is mainly for convenience, of course, and the fact that the splitter box often cuts off access to the mouse/keyboard and makes the monitor look yucky.

Of course, installing Ubuntu Server edition (CLI only!!) was a breeze; the installer is far more stripped down (it made me nastolgic for Slackware), but still very straight forward. It may scare the Linux n00b, but anyone who has installed a few Linux distros in their time would be comfortable with it.

So next up, I wanted to be able to SSH into my box from anywhere. This is both for convenience, and as I mentioned, removes the need to directly interact with the computer, which is a pain when it's sharing a monitor with my main Linux desktop (which, just to mention, is Mint :3). In order to accomplish this hardly daring feat, I created an account with, which is a free service and allows you to add your computer as a "host". I can't remember if there is a limit to the number of hosts you can add, but I'm inclined to say there is, since they have account upgrades that you need to pay for.

After doing this I installed ddclient (sudo apt-get install ddclient), which is essentially a daemon that keeps track of when your dynamic IP address changes, and notifies DynDNS of the change. DynDNS then updates you host so, voila, your hostname always resolves to the right computer! During the installation Ubuntu automatically configures ddclient for you, more or less. There is a small amount of hand-configuration, but you need only to change a few lines, and you're off and running!

After doing all this (extremely hard (; ) work, you should be able to directly SSH into your server box and admin from any computer you wish.

The next project I'm considering will be a bit more complicated, and more programming-oriented than server-oriented, so hopefully slightly more interesting :)