First, let's have a preliminary understanding of some points to note about references and pointers.
- A reference is not an object
- A reference must be initialized
- A reference can only be bound to an object, and cannot be bound to a literal value or the result of an expression
- Types must strictly match
int &a = 10; //错误:引用类型的初始值必须是一个对象 double a = 3.14; int &b = a; //错误:此处引用类型的初始值必须是int型对象
A pointer itself is an object
The type of the pointer must strictly match the object it points to
double dval; double *pd = &dval; //正确 double *pd2 = pd; //正确 int *pi = pd; //错误:指针pi的类型和pd的类型不匹配 pi = &dval; //错误:试图把double型对象的地址赋给int型指针
There is an exception: introducing the const qualifier
Constant reference
When initializing a constant reference, any expression is allowed as the initial value
int i = 42; const int &r1 = i; //正确:允许将const int & 绑定到一个普通int对象上 const int &r2 = 42; //正确 const int &r3 = r1 * 2; //正确 int &r4 = r1 * 2; //错误 double dval = 3.14; const int &ri = dval; //正确 //等价于 const int temp = dval; const int &ri = temp; int i = 42; int &r1 = i; const int &r2 = i; r1 = 0; //正确 r2 = 0; //错误
Constant pointer
Definition:Also called a constant pointer, it can be understood as a pointer to a constant. That is, this is a pointer, but what it points to is a constant. This constant is the value (address) of the pointer, not the value pointed to by the address.
Key points:
- 1. The object pointed to by a constant pointer cannot be modified through this pointer, but it can still be modified through the original declaration;
- 2. A constant pointer can be assigned the address of a variable; the reason it is called a constant pointer is that it restricts modifying the variable's value through this pointer;
- 3. The pointer can also point elsewhere, because the pointer itself is just a variable and can point to any address;
Code form:
Pointer to constant (
const int* ptr): The content pointed to by the pointer is a constant and cannot be modified through the pointer. However, the pointer itself (that is, the address stored in the pointer) can be changed.Constant pointer (
int* const ptr): The pointer itself (that is, the address stored in the pointer) is a constant and cannot be changed, but the content pointed to by the pointer can be modified.Constant pointer to constant (
const int* const ptr): Both the pointer itself (that is, the address stored in the pointer) and the content pointed to by the pointer are constants. Neither the pointer's address nor the pointed-to content can be modified.
Constant pointer
Definition:It is essentially a constant, modified by a pointer. Its own value (that is, the pointer's address) is a constant and cannot be changed. In other words, after initialization, the pointer variable always points to the same address. Although the pointer's address is fixed, the content it points to can be modified through the pointer.
Key points:The pointer itself (that is, the address stored in the pointer) is a constant and cannot be changed, but the content pointed to by the pointer can be modified.
Code form:
int* const p;
Constant pointer to a constant
Definition:A pointer constant pointing to a constant is a constant, and the object it points to is also a constant.
Key points:
- 1. A pointer constant points to a pointer object;
- 2. The pointer object it points to is also a constant, that is, the object it points to cannot change;
Code form:
const int* const p;
So how do we distinguish these types? The one with two const qualifiers is definitely a constant pointer to a constant, which is easy to understand. The main point is how to distinguish a constant pointer from a pointer constant:
One way is to look at the arrangement order of * and const, for example
int const* p; //const * 即常量指针 const int* p; //const * 即常量指针 int* const p; //* const 即指针常量
Another way is to see which one const is closer to, that is, read from right to left, for example
int const* p; //const修饰的是*p,即*p的内容不可通过p改变,但p不是const,p可以修改,*p不可修改; const int* p; //同上 int* const p; //const修饰的是p,p是指针,p指向的地址不能修改,p不能修改,但*p可以修改;
Example
//-------常量指针------- const int *p1 = &a; a = 300; //OK,仍然可以通过原来的声明修改值, //*p1 = 56; //Error,*p1是const int的,不可修改,即常量指针不可修改其指向地址 p1 = &b; //OK,指针还可以指向别处,因为指针只是个变量,可以随意指向; //-------指针常量-------// int* const p2 = &a; a = 500; //OK,仍然可以通过原来的声明修改值, *p2 = 400; //OK,指针是常量,指向的地址不可以变化,但是指向的地址所对应的内容可以变化 //p2 = &b; //Error,因为p2是const 指针,因此不能改变p2指向的内容 //-------指向常量的常量指针-------// const int* const p3 = &a; //*p3 = 1; //Error //p3 = &b; //Error a = 5000; //OK,仍然可以通过原来的声明修改值
In practical applications, constant pointers are used more often than pointer constants. For example, constant pointers are often used in function parameter passing to avoid modifying the content inside the function.
size_t strlen(const char* src); //常量指针,src的值不可改变; char a[] = "hello"; char b[] = "world"; size_t a1 = strlen(a); size_t b1 = strlen(b);
Although a and b can be modified, it can be guaranteed that the contents of a and b will not be modified inside the strlen function.
Null pointer, wild pointer
Since we have talked about pointers, let's also mention the issues of null pointers and wild pointers.
A null pointer is a pointer that stores an empty address. When using a pointer, you must first determine whether it is a null pointer; many problems are caused by this step.
A wild pointer is one that, after the pointer is deleted, is not set to 0, causing the pointer to arbitrarily point to a memory address. If it continues to be used, it will cause unpredictable memory errors.
In addition, misuse of pointers can easily cause bugs or memory leaks.
Look at the code:
//-------空指针-------//
int *p4 = NULL;
//printf("%d",*p4); //运行Error,使用指针时必须先判断是否空指针
//-------野指针(悬浮、迷途指针)-------//
int *p5 = new int(5);
delete p5;
p5 = NULL; //一定要有这一步
printf("%d",*p5); //隐藏bug,delete掉指针后一定要置0,不然指针指向位置不可控,运行中可导致系统挂掉
//-------指针的内存泄漏-------//
int *p6 = new int(6);
p6 = new int(7); //p6原本指向的那块内存尚未释放,结果p6又指向了别处,原来new的内存无法访问,也无法delete了,造成memory leak
Reference address:
http://blog.csdn.net/Rueing839/article/details/48416907
http://www.cnblogs.com/lizhenghn/p/3630405.html