Pointers.  What are pointers and what’s so important about them?
A pointer is a location in memory which is used, not as the data you want to use, but as the address indicating the location of that data.  The difference is critical, and many people who have not used pointers (because the programming language they use doesn’t support them) or do not understand them (because they don’t know how), just don’t get them.
A noted programmer and blogger, Joel Spolsky, points out that when he interviews someone for a potential programming job, that whether someone uses pointers is the “acid test” for whether they’d be much good as a programmer.  In his case, it’s understandable; his shop uses the C and C++ language, where pointers are almost mandatory for any program beyond anything which is merely a trivial program or is an application of any reasonable size.
The use of pointers, in a language that supports it, gives one a power and capacity that might not otherwise be available with the use of any other data type.  Since a pointer is not data, but the location of data, it provides indirection to data and can be used as a placeholder for the data.  This can be useful for storing things such as lists of strings or other variable-length items.  Each string can be a pointer, and the string can either be a “Pascal-type” string (where the string has one or more bytes preceding it which indicate its length) or a “C-type” string (where the string is of any length but is terminated by a byte of 0).
If you were storing a group of strings, and you used arrays, you would have to allocate the arrays in advance (or find some way to have dynamic arrays that change), and you would have to allocate the strings in each entry.  Some languages require each element of an array to be the same.  This can either mean having to create lots of wasted space, or having to set up some scheme to provide for dynamic strings.
If, however, you were using a list, where each string was indicated by using a pointer, any time you want to alter a string, you can simply change the pointer to the address of the replacement string.  Without using pointers, you have to do string moves every time you change something.  This can significantly improve the speed of the application.
If you were designing a word processor, if each paragraph is a pointer, when a paragraph is edited, you create a new pointer, replace the old one, and let the system handle recycling the memory used by the old paragraph.  If you use strings, arrays, or any other direct data structure, if you change a paragraph, you have to actually move the whole text of the paragraph.  Moving pointers is often implemented as a single instruction – and very fast – on most computers.  String moves are very expensive by comparison.
The big disadvantage to pointers is that, whatever you’re using those to point to should be larger than the size of the pointer, otherwise you’re using much more room to handle the items than the items themselves actually use.  It’s also faster to move very small items (one or two bytes) than it is to move a pointer (typically 4 or 8 bytes, depending on whether it’s a 32- or 64-bit machine.

Blog Widget by LinkWithin

Leave a Reply