The Huz Experience

RantsTecho Techno Techno!

Pointers? Pointless more like.

Monday 17th April 2006

C++ is a confusing, excessively low-level programming language for someone used to the relative safety and comfort of the well-designed, garbage-collected world of Java or C#.

It makes a distinction between objects declared locally and objects declared on the heap. Local objects disappear, Java-style, as soon as they go out of scope. All well and good. For every object you declare on the heap, you get a pointer back. You must keep track of these - if you lose them, the objects they point to aren’t gone; they hang around, clogging up vital memory. You just can’t access them any more.

There’s a document entitled “How to Shoot Yourself in the Foot”, which serves as an amusing comparison of various programming languages. The entry for C++ says:

You accidently create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can’t tell which are bitwise copies and which are just pointing at others and saying “That’s me, over there.”

Sounds about right to me. You see, multiple pointers - even multiple pointers passed between multiple objects - can quite happily point to the same object, which makes for extra headaches when it comes to deletion time.

Delete the object from the ‘wrong’ pointer? All the other pointers are now pointing to invalid areas of memory, and your program crashes when it tries to dereference them. Forget to delete the object at all? Your program sucks up more RAM than OpenOffice on a spreadsheet binge, and the only way to free it is to restart your program.

But the worst thing about pointers is how useful they can be, or so I’m told. No more creating copies of objects to pass around, even if it does happen automatically - so inefficient, pfft! - you can just refer to the exact same thing from multiple places!

It’s just a shame it gets incredibly confusing fast.


Leave a Reply

Hey there. The Huz Experience would be a right pain to administer without WordPress, and would be overrun with spam for questionable knob potions without Akismet. Thanks chaps!

Valid XHTML 1.0 Strict   Valid CSS!