Why every C or C++ programmer must know pointers

March 4, 2013 at 9:00 am | Posted in C++, Programming | Leave a comment
Tags: , ,

Some computer science engineering students once asked me why it is so important to know about pointers in C and C++. They complained (and almost every computer programmer in industry complains) that pointers are so hard to learn and they can’t see why they should work so hard. I was travelling in MMTS (Local Train Service in Hyderabad). MMTS service uses several trains and compartments in few trains have a LED-Box where the name of railway station is displayed before train is about to reach the same. And here is how it shows railway station name sometimes :

Do you see the name there ? Those letters which look like boxes and Ys with tildes above on them are actually memory corruption rather than English language. Now I can guess the program must have been written in a language where programmer had to do memory management himself. Since it is a LED-Box, it must be a C or C++ prgram in embedded environment. This is a real-life result of not knowing how to respect memory constraints. My best guess is either prgroammer has some dangling-pointer in his code or he must have an array-overrun (a.k.a Buffer-Overflow). Imagine these situations:

  • Your C++ program does financial transactions for some company e.g. distributing salaries to its employees and your program uses a dangling-pointer to set salary amounts. With this dangling-pointer the CEO might get salary of a blue-collar worker or vice-versa.
  • Your C program is used in controlling a weapon like firing, pausing, continuous 20 seconds firing etc. With buffer-overflow gun may fire for 20 seconds continuously when you press pause.
  • Imagine your C program controlls a satellite orbiting around Earth being controlled from base station somewhere on Earth. Memory corruption can cause satellite to move left instead of right or even stop its motion or may burn all of its fuel while it orbits non-stop at full speed around earth.

These are some of the not so serious consequences of not knowing pointers. So, if you want to write correct C and C++ code, learn howe to use pointers. Yes, they are hard to learn and more than hardness, pointers requires experience. It is a very useful skill that will reduce debugging sessions from days to hours. Kernighen and Ritchie said in the preface of their book “The C Programming Language” printed in 1978 that C wears well as one’s experiecne with it grows. When 2nd edition was printed in 1988 (affectionately known as K&R2) they said they still feel same about C with decade more experience. They had a point (they had pointers in those 10 years 😉 ). Experience makes a good C programmer and you achieve that by following the footsteps of masters. In order te be a brilliant C programmer, You need to follow brilliant masters. Start with c-faq.com

Copyright © 2013 Arnuld Uttre, Village – Patti, P.O – Manakpur, Tehsil – Nangal, Distt. – Ropar, Punjab (INDIA).
Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.


Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.