Pointer Manipulation in C Plus Plus
The pointer data type in C Plus Plus (C++) provides a technology for directly manipulating computer memory during program execution. Pointers offer tremendous power along with unprecedented opportunities for catastrophe. In this tutorial we introduce basic techniques for pointer manipulation in C Plus Plus. We provide several pointer examples in code. Each pointer example includes a commentary. We also point out some potential pitfalls.
Example Code
Consider the following example code. Line 4 illustrates a declaration statement for a data item called alpha. The understanding of data types when dealing with pointers is crucial. The data type of alpha is not 'char'. Alpha is a "pointer to a char".
As the code is written, alpha is initialized to point to the "H" in "Howdy". The "H" in "Howdy" is a char data item; alpha contains the memory address of that char data item. By definition, the C++ compiler ensures that the letters in the string "Howdy Everyone" will be contiguous in memory when the program executes. Therefore we will be able to manipulate the string through the pointer alpha.
// Pointer Manipulation in C Plus Plus (C++) #include <iostream> // alpha is a "pointer to a char" char *alpha = "Howdy Everyone"; char beta[] = "Hello World"; void main() { }
Using the pointer to access the array
In the following code block we add logic to step through the string using the pointer. We use the "++" operator in line 13 to increment the pointer. This will cause the pointer to point to the next char data item in memory. The increment works properly because the compiler 'knows' that alpha points to a char. Since a char is typically 1 byte wide, the pointer is incremented by 1, to point to the next contiguous byte in memory.
Note: the exact amount by which the pointer is incremented is a function of several factors that are beyond the scope of this tutorial.
Had alpha been an int data item, the "++" operator would probably cause alpha to be incremented by 4 (assuming an int data item is 4 bytes wide).
// Pointer Manipulation in C Plus Plus (C++) #include <iostream> // alpha is a "pointer to a char" char *alpha = "Howdy Everyone"; char beta[] = "Hello World"; void main() { int i for (i = 0; i < 5; i++) { // *alpha can be read as "thing pointed to by" alpha std::cout << *alpha; alpha++; } }
What can go wrong?
Referring again to the previous code, we can easily introduce a significant logic error by changing the loop limit in line 10. Our original example limited the loop to 5 passes: 0 through 4. This causes the first 5 values in the string to be processed. In other words, the program prints out "Howdy" (quotes added for clarity) and stops.
Unfortunately, a simple typing error may create a problem that could take years to surface. Consider this simple modification to line 10 of our previous code:
for (i = 0; i < 15; i++) {
Maybe it will work
Adding a (mostly) harmless one to the loop limit will cause the logic to cycle trough the loop 15 times, i = 0 through i = 14. When writing code we always ask ourselves two questions:
1. Will it compile? In other words, is the syntax correct? For our 'modified' example, the answer is yes, it will compile. It compiles because we have introduced a logic error, not a syntax error. Syntax errors are easy to spot; the compiler flags them during the compilation process. Logic errors are much more insidious.
2. Will it execute? In other words, is the logic correct enough to run without crashing? For our 'modified' example, the answer is 'maybe'. We do have a logic error, but it may or may not manifest in a dramatic manner.
Consider the declaration of the pointer and the associated initializer:
// 1 // 012345678890123 char *alpha = "Howdy Everyone";
The initializer contains 14 characters (13 letters and an implicit null terminator). However, our code expects to traverse 15 characters. In this particular example, depending on the particular C++ compiler we are using, the program will step into the next data item in memory. Based on the way we laid out the code, that will probably be the array beta[], but there's no guarantee of that.
To summarize, the code will run without crashing if there's a data item following alpha that our program has permission to access. If such a data item is not present or if our program attempts to access memory outside it's assigned memory space, it may crash.
This type of error may not be detected for many years of 'proper' program execution. Oddly enough, this particular program may run properly on some computers and not on others, depending on the compiler and the target operating system.
Conclusion
We have demonstrated basic pointer manipulation in C Plus Plus (cpp) and possible pitfalls.