[C ++] layman's language C ++ pointer arithmetic and pointer safety summary

Pointer arithmetic

Pointer p may be plus or minus an integer n, but the significance of this operation and the normal pointer value plus or minus
sense operation is not the same, which is based on a unit basis, such as p-type pointing int integer, one unit issizeof(int)

(1) Example 1

char a[20]="You are my friend";
int *ptr=(int *)a;
ptr = ptr + 3; 

Each unit is a byte char array, and pointed to by ptr pointer unit is 4 bytes
it did not add to an array. 1 a first prior ptr 3 - 4 bytes (as a whole), add 3 , a PTR has an array of points 13 - 16 bytes, i.e.,指向a[12] - a[15]这部分

(2) Example 2

#include <iostream>

using namespace std;

int main() {

    int array[20] = {0};
    int *ptr = array; // 数组名array是地址,把它赋给指针ptr

    for(int i = 0; i < 20; i++) {
        *ptr = i;
        ptr++;
    }

    ptr = array;
    for (int i = 0; i < 16; i += 4) {
        cout << *(ptr + 4) << endl;
        // 等价于cout << ptr[4] << endl; 
        ptr += 4; 
        // 因为ptr指向的是int,而数组array也是int
        // 所以指针ptr+4,等于下标i+4
    }
    return 0;
}

Summarizes
a pointer (address) ptrold After the addition (subtraction) an integer n, the result is a new pointer (address) ptrnew ,
ptrnew type and of ptrold same type, ptrnew points and the type ptrold type points are also the same

Ptrnew ptrold value than the value of the increase (decrease) of n by sizeof (type ptrold pointed) bytes

That is: ptrnew points ptrold memory area than the memory area pointed to by n moved sizeof (type ptrold pointed) bytes to the high (low) direction address.

Pointer and pointer subtraction:

  • Two pointers are not added, it is an illegal operation
    because after the addition, the results obtained to the point where an unknown, and no meaning.
  • Subtracting two pointers may
    but must be the same type, can be used for obtaining the size of the array, of course remember divided by sizeof (type)

Relationship between arrays and pointers * (p + 4) and p [4]

Array name array is essentially a pointer,

int array[]={0,1,2,3,4,5,6};
int *p = array; 

Remember that
* ( P + . 4 ) , and P [ . 4 ] is equivalent to

* ( Array + . 4 ) and Array [ . 4 ] is equivalent to

And array[0]equivalent to p[0], since p = array;
So in fact, is equivalent to the above four

Relationship between structures and pointers p-> data; and (* p) .data

struct node {
	int data;
	int time;
}node;

node Tu;
node *p = &Tu;

Just remember: (* the p- .) The Data and the p- -> the Data; is equivalent

Pointer security issues

(1) Example 1

int a;
int *p = &a;
p += 1;
*p = 8;

The above code exists serious error, since the third line p += 1after execution, P points to an integer variable storage region adjacent to a higher address direction, and a fourth we actually write data to the line in this region , which is very dangerous, because we do not know what it is inside the original content , the system is very important in case of data it

Array int a [10] Similarly operation possible, it is because we have applied for a continuous length of space 10, this space is known and we can freely write data

So, we must be very clear when using the Pointer: This pointer points to exactly where

(2) Example 2

When we use the pointer casts p1 = (type *) p2, if sizeof (p2 type) greater than sizeof (ptr1 type) when you use the pointer p1 to access the storage area p2 points to be safe. Otherwise there will be serious problems

char t = 'a';
int *p;
p = (int *)&t;
*p = 520;

Run the code may not be an error, but there are security risks , because p points to characters aand three bytes and its adjacent higher addresses, and three bytes of content that we do not know what is behind, in case it is very important

(3) The following code is safe and can explain my machine is little-endian mode

Because the last value t 48, we can see that the point p bytes int4 low byte forcibly converted char pointer p is a pointer to the default bytes int4 low byte address, it is possible Description my machine is little-endian mode (low byte at the lower address)

 int t = 7;
 char *p; 
 p = (char *)&t;
 *p = '0'; // 对应48:00110000  char 一个字节:0-127
 cout << *p << endl; 
 cout << t << endl; // 输出48
Published 239 original articles · won praise 80 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_43827595/article/details/104344130