No more mallocs, pointers or linked lists

08 Aug

I started with HTML which isn’t really a language but it was a definite milestone.  Then came irc scripting and on to c++ and some vb.  In college I started .net and dabbled in some php.  This is just a summary but the point is I progressively worked my way up and into the modern languages until my first career.  For my first 2+ years I programmed desktop software written in c.  It was a very mature and complex program.  I threw me off my game quite a bit.  The three previous years I had almost exclusively written web applications that used some form of database software.  I was forced back in time to a land of custom “frameworks” and what I would refer to as “hard core programming.”

A pointer… just think about it.  Picture something really cool that you want.  Now picture your friend starring at it.  If you didn’t know where that really cool thing was you could look and your friend and he would tell you where it was.  So your friend is pretty much useless by himself but as long as he is starring at what you want you’ll keep him around.  But if your friend dazes off and looses track of the object of your desiring you may as well consider him useless… or NULL.  NULL pointers are dangerous.

A malloc is a simple subroutine that was mostly phased out by the “new” operator in later languages.  The idea was you could reserve a block of memory to store whatever you wanted into it. The malloc() function takes a parameter for the size you would like to reserve and returns a pointer to the memory location.  Avoid memory leaks at all costs. It is always important to keep good track of the memory you are using.  If you are not using it, free it. The problem occurs when you need to reuse a variable but you actually just create room for a new one.  The old variables pile up until the system runs out of memory.

I recall linked lists were a tough concept for me to grasp.  It wasn’t because its a difficult concept, I just couldn’t understand why you wouldn’t use a database to store your records.  Maybe enforce some referential integrity? No, not in the C era, unless you do it manually.  For those of you who know databases but are curious about linked lists picture a results set in SQL, 100 records and 10 fields.  They way you would store and scan this in C is sequentially.  The first record would have your root pointer at the beginning.  Each record would have a pointer to the next record.  And the very last record’s next pointer would be NULL.  If you need to find all records who’s last name = smith you could do something like this:

while (rec != NULL; rec = rec->next)
if (rec->lastname == smith)
You can imagine how fun things got when you had to insert/update/delete records.  The ACID properties were pretty much out the window.
Leave a comment

Posted by on August 8, 2010 in Uncategorized


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: