Uninitialized Pointers


1.Uninitialized Pointers

The classic example of a pointer error is the uninitialized pointer. Consider this example:
// This program is wrong.
int main(){
int x, *p;
x = 10;
*p = x; // where does p point?
return 0;
}


Here, p contains an unknown address because it has never been defined. You will have no way of knowing where the value of x has been written. When your program is very small, as it is here, the odds are that p will contain an address that is not in your code or data area. Most of the time, your program will seem to work fine. However, as your program grows, the probability of p pointing into either your program’s code or data area increases. Eventually your program stops working. The way to prevent this type of program is obvious: make sure that a pointer is pointing to something valid before using it!
Share on Google Plus