I. pointer 1. Delete pointer
(1) After the pointer is deleted, the pointer must be null.
It is invalid to use a null pointer, which may cause program crash.
The pointer is null to facilitate the check of errors.
(2) In Class destructor, after deleting the pointer, the pointer can be not cleared. Because after the object disappears, the member pointer of the object becomes inaccessible.
(3) This pointer records the address of the current object.
This pointer is created and deleted by the compiler.
2. addition and subtraction of pointers
When you add or subtract a pointer, the memory address changes accordingly based on the pointer type.
class A{int x;};int main(){A *p = new A;cout<<p;p++;cout<<' '<<p<<endl;int *q = new int;cout<<q;q++;cout<<' '<<q<<endl;double *w = new double;cout<<w;w++;cout<<' '<<w<<endl;return 0;}
Output:
00741df8 00741dfc // the space occupied by an object of Class A is 4B
00741e68 00741e6c // One int type variable 4B
00740848 00740850 // the space occupied by a double variable is 2B ??????????????????
3. Several pointers combined with Constants
Name |
Expression |
Address of the pointer |
Content pointed to by pointer |
Constant pointer |
Int * const P; |
Immutable |
Variable |
Pointer to a constant |
Const int * P; |
Variable |
Immutable |
Constant pointer to a constant |
Const int * const P; |
Immutable |
Immutable |
Ii. References
1. The reference is an alias, such as Int & rnum = num;
Example 1:
int main(){int a;int &ra = a;ra = 999;cout<<"a:"<<a<<endl;a = 0;cout<<"ra:"<<ra<<endl;cout<<&a<<' '<<&ra<<endl;return 0;}
Output:
A: 999 // Ra change will cause a to make the same change
RA: 0 // changing a will cause Ra to make the same change
0012ff44 0012ff44 // RA and a have the same address
1) Ra is the alias of A. Ra is.
2) The significance of & RA in L4 and '9' is different.
L4: indicates that Ra is a reference.
9: Get the address of RA
2. An object alias can be defined, but a class alias cannot be defined because the class has no address.
3. When defining a reference, you must initialize the reference at the same time. A reference is like a constant. It can only be initialized and cannot be assigned a value.
Example:
Int main () {int A; Int & RA = A; // The correct int B; Int & RB; RB = B; // error return 0 ;}
4. int * P, * q; // both p and q are pointers.
Int * p, q; // P is a pointer and Q is a variable.
3. pointer and reference 1. pointer and reference interval(1) Pointers are indirectly accessed because * is used to read the data at the address. Using a pointer as a function receiving parameter is indirectly referenced.
A reference is a direct access. Because the reference is an alias, the data of this object can be directly read without any symbols. The alias is directly referenced as the receiving parameter of the function.
(2)
|
Pointer |
Reference |
Can it be blank? |
Yes |
No |
Can be initialized? |
Yes |
Yes |
Can I assign a value? |
Yes |
No |
Can I access the heap? |
Yes |
No |
No, the object can be changed. |
You can change the value of an object. |
The referenced object cannot be changed, and the object value can be changed. |
2. Examples of separate use of pointers and references
(1) Example 1: Int & R = new int; // error. It cannot be referenced directly to point to the heap space.
(2) Example 2:
Int * P = new int; Int & R = * P; // correct. r is the alias r = 4 For an untitled int variable read through the pointer P; cout <* P; // output 4
(3) Example 3: int * & R = new int; // correct
Create a heap space and define R as the reference of the space address. In this way, r becomes the alias of the space address. Because the reference cannot be used as a pointer, add * Before & R *. In this way, r becomes a pointer variable, which stores the address of the space in the heap. Therefore, if you use * to access R, * r is to access data in the heap. Use * to read the value at the R address. * & R is the address in the read heap.
3. Examples of separate use of pointers and references
Int main () {/* If int application fails-> NULL pointer is returned-> a useless reference cannot be blank, reading a useless reference with * Will reference system crash */int * & R = new int; * r = 6;/* r is the alias of New Int, it is also the alias of the new space address. Use * to access R, is to read the data stored in the new space */cout <"read the value at the address of the new space" <* r <Endl; /* The R value is the address of the new space */cout <"output address of the new space" <r <Endl;/* read the value saved by the R address with * r, first use the & get address, and then use * to read the value of this address */cout <"to read the value of the r address" <* & R <Endl; /* r also has its own address */cout <"output r memory address" <& R <Endl; return 0 ;}
Output:
Value 6 at the address where the new space is read
Output address of the new space 006a1df8
Output r memory address 0012ff40