
Dear Larry,
My pointer code keeps crashing my program. Can you offer me any assistance?
Oscar S.
Hello Oscar,
While incredibly useful and powerful, pointers can also be a treacherous minefield in a program and even the best programmers will make mistakes. First, we should understand that a pointer stores the address of a piece of data.
A Handy Line of Code for Debugging Pointers
Here is an example that should make this clear:
main()
{
int i;
int *MyPointer;
i = 11;
MyPointer = &i;
//**************GOOD FOR DEBUGGING POINTERS**********************
printf("\nAt address 0x%x, the value %d is stored\n\n", MyPointer, * MyPointer);
//****************************************************************
}
The printf() statement used in the program above can be an invaluable way of debugging because it allows you to see both the address stored in the pointer and the data at the memory location indicated by the pointer.
Pointers gone Wild
Consider the following code:
main()
{
int i;
int *GoodPointer; //The good guy will be initialized
int *BadPointer; //The bad guy won’t be initialized
GoodPointer = NULL; //Set it to a known value just to be safe
i = 11;
GoodPointer = &i; //Set the address of the pointer
*BadPointer = 22; //WRONG!! We just wrote over some random memory!!
printf("\nAt address 0x%x, the value %d is stored\n\n", GoodPointer, *GoodPointer);
//MyPointer2 is a WILD POINTER
printf("\nAt address 0x%x, the value %d is stored\n\n", BadPointer, *BadPointer);
}
What is the purpose of this code? Remember that an uninitialized variable contains junk data that may have been in the memory at the time. By definition the value stored in the pointer when it is created is bad data which might look like a valid address. Setting your pointer to NULL ensures you have a known starting condition.
Many of the most common mistakes in pointer programming come from an uninitialized pointer that reads or writes data with an invalid memory address. A pointer that hasn’t been initialized is called a Wild Pointer and you can imagine what kind of terrible things that might cause in your code when you try to use it for something.
Fortunately, Dynamic C should provide you with a compiler error when you try to use a pointer that isn’t initialized, but even then initializing a pointer correctly is very important.
Array of Sunshine When Debugging
Arrays are another place where pointers can sneak up and bite you. Most compilers will recognize when you work with an array that you shouldn’t be allowed to walk past the last element of the array and it will produce a compiler error. If you direct a pointer to the array, you can happily keep going past the end of your array and overwrite data you intended to keep. Take a look at the following two similar programs.
main()
{
int i;
char MyString[20];
//This for loop will throw a compiler error!
for(i=0;i<30;i++)
{
MyString[i] = ‘R’;
}
}
The code above shouldn’t compile but the code below will and that is very bad.
main()
{
int i;
char MyString[20];
char *MyPointer;
MyPointer = MyString;
//DANGER! DANGER! This for loop WILL NOT throw a compiler error!
for(i=0;i<30;i++)
{
*MyPointer = ‘R’;
MyPointer++;
}
}
The pointer code is much more dangerous because the programmer has the power to overwrite memory with impunity. When you work with pointers, it is very important to be aware of any boundary conditions like the one on this array. If you unintentionally go past the boundary, the best thing that could happen is that your code will crash immediately. The worst thing that can happen is that it might seem to work but your pointer is actually overwriting some other variable. You should be careful of the same thing when using an array to point to any data structures as well.
The pointer code is much more dangerous because the programmer has the power to overwrite memory with impunity. When you work with pointers, it is very important to be aware of any boundary conditions like the one on this array. If you unintentionally go past the boundary, the best thing that could happen is that your code will crash immediately. The worst thing that can happen is that it might seem to work but your pointer is actually overwriting some other variable. You should be careful of the same thing when using an array to point to any data structures as well.
- Larry C.
Larry Cicchinelli is Rabbit’s Technical Support Manager. He has 30 years of embedded experience, and is considered one of the foremost authorities on Rabbit products. Larry and his staff offer comprehensive technical support to Rabbit customers.
Submit your questions for Larry via email at
AskLarry@rabbit.com
Read more Ask Larry Answers