Thursday, September 18, 2008

Null Pointers and Large Spiders

So, today, I borked the game temporarily by doing something stupid. I mentioned this to the guy sitting at the cube next to mine who is an animator and I was faced with the task of explaining to him exactly what a null pointer error was.
It was obvious from the blank look in his eyes that I was doing a bad job of this.
At least, I thought later, I could make my nonsensical explanation amusing.
(So I came up with this)
So, say you want to get a pet tarantula. You go to the pet store, and you can either get a cage and get the tarantula later -
Tarantula *beulah;
Or, you can get the cage and put a tarantula in it.
Tarantula *beulah = new Tarantula();
The cage is big enough for the tarantula to get really hideously big and wander around and stuff, when you decide to stick the tarantula in it. Note that you can't get a tarantula without a cage, because it would inevitably escape and you would have no bloody clue how to find it, and the idea of having a tarantula running around on the loose is against the general order of the universe.
Above musings on tarantulas aside, let's say you get a tarantula cage without the tarantula, and you take it home and set it up.
Then you try to hold the tarantula.
This is the point where you become totally batguano crazy, or if you are a computer, you crash.
You have a null pointer error....the the computer has given you a chunk of memory space for your tarantula, assuming that if there's a tarantula cage there's going to be a tarantula living there, but there is no actual tarantula at that memory address.
So hopefully I've confused you even more with this dumb analogy and given myself job security. It's all part of the plan.

3 comments:

Owen said...

Technically, in C++ the code Tarantula* belulah; does something even worse than this: C++ will leave this pointer completely uninitialized; i.e. its value could be anything. It's not a null pointer at that point (a null pointer is defined as a pointer whose value is 0), it's a garbage pointer. Dereferencing it would therefore be somewhat akin to going in to grab a tarantula and instead grabbing a shark. Or worse--grabbing a shark and deluding yourself into thinking it's really a tarantula. If you're really really unlucky, you might pet the shark and take it out to show your friends before you realized your tragic mistake.

I'm not sure medical insurance even covers those kinds of mishaps.

Scrabcake said...

I am actually aware of that distinction. I just tend to refer to this particular error as a Null Pointer, mostly because I'm a java hack and used to that terminology, and also because they both have the same cause--you didn't initialize your stuff. They also end up in roughly the same situation. Your game tried to do something with something it thought was there but which wasn't. Anyways, I apologize for the terminology. Thanks for the correction.

Unknown said...

Holy smokes! Snakes on a PLANE!
Err.. Spiders? Anyway.. that sorta makes sense to me. :)