13
Mar

Jesus wept. Not because he was to one day die on the cross, but because you still suck at using pointers. In fact, Jesus and I had a thrilling conversation about your coding incompetence last night over a bottle of 1978 Chateau Margaux.

As Jesus took the last of sip of wine, he let out a deep, exasperated sigh and explained to me that I had to continue my discipleship of pointers. It was not my fault that you all suck - Jesus simply made me better than you. I supposed some people are blessed while others suck at pointers. Oh well. Lets continue shall we?

Just like in Java, variables can be created in two places - the stack and the memory heap. We do not have to worry about managing stack variables. They are created when a function is in scope, and destroyed when the function falls out of scope (aka the function returns).

Unlike Java, C++ assumes you have a tad more intelligence and can actually pay attention to the code you wrote five lines ago. When allocating variables on the memory heap, managing the memory is left up to the coder. Java used a method called "garbage collection" that did this for you - welcome to the big leagues now.

Meet new. He is your friend. To allocate a variable on the memory use new. The result is a pointer to an object on the memory heap, as opposed to a variable allocated on the runtime stack.

  • 1.
  • string *hacker;
  • 2.
  • hacker = new string("i am 1337");
  • 3.
  • cout << *hacker << endl;
  • 4.
  • cout << (*hacker).length();
  • 5.
  • delete hacker;

This example is very simple. First a pointer of string type is created. Then, we use hacker pointer to allocate a string on the memory heap with the value "i am 1337". Afterwards, the string and the length of the string are printed out. And yes, the parentheses are very important. Remember "precedence" from the first entry? You should. However, using the parentheses is not considered good syntax. C++ provides the -> operator which access members of a pointer, providing the pointer is pointing at an object class type. Printing the length of the hacker variable can be written as:

  • 1.
  • cout << hacker->length();

I cannot stress this next point enough. DELETE IS VERY IMPORTANT. Calling delete on a pointer variable when you are finished using it is essential to good programming practice for several reasons. First, it is good form. Second, and most important, calling delete returns the used memory. If the pointer is not deleted then the memory allocated is lost, causing a memory leak. We do not want that. Memory leaks are the death knell of most programs, and to make matters worse they are tricky to spot, so make sure you keep track of your pointers that are allocated on the memory heap. In most cases, if a variable can you used on the stack, create it there rather on the heap.

For example, a type of programming bug is "stale pointers". Assume the following lines of code are scattered throughout a program.

  • 1.
  • string *hacker = new string("i am 1337");
  • 2.
  • string *pointerHacker = hacker;
  • 3.
  • delete pointerHacker;

First hacker is initialized to point to a string on the memory heap. pointerHacker is set to equal hacker - they both point to the same place in memory. Calling delete pointerHacker then frees the memory allocated, rendering the hacker pointer stale - it no longer points to a valid place in memory. To further complicate matters, if new is called while hacker is stale, then it is very possible for the memory that hacker pointed to is claimed. The bug would not be obvious when looking at code, but during runtime there will be noticeable errors.

Another type of bug bound to cause runtime errors is calling delete twice on a stale pointer - the pointer no longer points to a valid object in memory.

As you can see, memory management in C++ is much harder than it is Java, but it is quite do able as long as you PAY ATTENTION. It may be hard, but try to lay off the whiskey sours and Zima while trying to code a program with pointers. Your intelligence is already skewed enough, lets not add to it.

If you still need help on the basics of pointers then send me an e-mail. DO NOT send another message to Jesus. He is a great guy and all, but frankly I am not a big wine drinker. I know he can make it whenever he wants, but his whole miracle montage is too much.

Note: This blog entry is a play off of the hysterical Donnie Hoyle and his You Suck At Photoshop Series.

name:
e-mail:
website:
comment:
creativecommons.org by-nc-nd
© chaoscoding 2008