Principle of c++ wild pointer and how to avoid it

  • 2020-11-18 06:22:33
  • OfStack

Definition 1.

A pointer to an invalid memory address is called an Wild Pointer or a suspended pointer, Dangling Pointer, meaning a pointer that does not work properly.

2. The common situation of wild pointer

2.1 Use uninitialized Pointers

The most typical case of a wild pointer occurs when a pointer variable is not initialized after it is defined, as shown in the following program.


#include <iostream>
using namespace std;

int main()
{
  int* p;
  cout<<*p<<endl; // Compile passed, runtime error 
}

2.2 The object referred to by the pointer is dead

A runtime error occurs when a pointer is used to access an object after the object's life cycle has ended and the object has died. Consider the following procedure.


#include <iostream>
using namespace std;

int* retAddr()
{
  int num=10;
  return &num;
}

int main()
{
  int* p=NULL;
  p=retAddr();
  cout<<&p<<endl;
  cout<<*p<<endl;
}

The above program is compiled and run without errors, and the output result is as follows:

[

001AFD48
1701495776

]

In the last line, the output is not the imaginary value 10 of num, because the variable num is a local variable stored in the stack space and is released when it leaves the function beyond its scope, so the output value is an inexact value.

Note:
(1) If cout<<&p<< endl ; To output num as a value of 10, or to cout<<*p<<endl ; The memory space of the local variable num is reclaimed after the function retAddr() is called, but its value has not been modified cout<<&p<<endl ; It's actually a member function that calls the cout object ostream& operator<<() , reusing the stack space used during the retAddr() call, the memory space of num is overwritten and the indeterminate value is output.

(2) Modify the value of the memory space pointed by p to enable normal compilation and operation.


int main()
{
  int* p = NULL;
  p = retAddr();
  *p = 11;
  cout << *p << endl;
}

The above code outputs 11. The address space that p points to here does not belong to the stack space of main function, but the operating system preempts 1 section of available stack space for user programs to use when the program runs. In general, Windows defaults to 1M and Linux defaults to 10M. The pre-created stack space is not a system protective address and can be overwritten and accessed by any program. Therefore, the value of the memory space pointed by p can be changed and the output can be accessed.

2.3 No null after pointer is released

The pointer p is not set to NULL after free or delete, making people mistakenly believe that p is a valid pointer. By free and delete, the pointer frees the memory space indicated by the pointer, but does not empty the pointer itself. In this case, the pointer points to "garbage" memory. The freed pointer should be set to NULL immediately to prevent the generation of wild Pointers. Consider the following procedure.


#include <iostream>
using namespace std;

int main()
{
  int* p=NULL;
  p=new int[10];
  delete p;
  cout<<"p[0]:"<<p[0]<<endl;
}

The output of the program is 1 random value, because the pointer to the space is garbage memory, holding the random value.

3. How to avoid the appearance of wild pointer

Wild Pointers are sometimes hidden from the compiler. To prevent the harm they can do, developers should pay attention to the following points.
(1) C++ introduced the reference mechanism. If reference can be used to achieve programming purposes, there is no need to use Pointers. Because references must be initialized when they are defined, wild Pointers can be avoided.

(2) If 1 must use a pointer, it needs to be initialized at the same time as the pointer variable is defined. Set it to NULL or point to a named variable when defined.

(3) Set the pointer to NULL after free or delete operation. In the case of free, it is common to define a macro or function xfree to replace free with a null pointer:


#define xfree(x) free(x); x = NULL;

That's how c++ Pointers work and how to avoid them. Read more about c++ Pointers in other articles on this site!


Related articles: