C + + pointer: (including two-dimensional array, two-level pointer, dynamic two-dimensional array, memory continuity)

Posted by safra on Thu, 27 Jan 2022 04:53:28 +0100

C + + pointer: (including two-dimensional array, two-level pointer, dynamic two-dimensional array, memory continuity)

In the creation and use of C + + array, this paper briefly introduces the use of pointers in the array, but there are some other problems. Here we mainly expand some about two-dimensional array pointers, secondary pointers and the memory continuity of dynamic two-dimensional array.

1. When any pointer variable is created, it will not automatically become a NULL pointer. Its default value is random and it will point at random. Therefore, if it is not used in the short term, first assign the pointer to NULL, for example: int * p = null;

2. From the perspective of memory requirements, different types of pointers are not different. Their occupied space is related to the operating system (32-bit operating system pointer occupies 4 bytes and 64 bit system pointer occupies 8 bytes). The difference lies in the different object s they address. In other words, the pointer type will teach the compiler how to parse the memory content and machine size in a specific address.

3. Difference between primary pointer and secondary pointer

int *p refers to the first level pointer, which means that the address pointed to by p stores a value of type int.

int **p refers to the secondary pointer, which means that the address pointed to by p stores a pointer pointing to int type.

The first level pointer stores the address of the variable, and the value pointed to is the content of the variable. For example, int* p1={1,2,3}, p1 = the first address of the array, * p1 = the first value of the array;

The secondary pointer stores the address of the primary pointer and points to the primary pointer. For example, int*p1 ={1,2,3}, int * * p2 = & P, p2 = the first address of pointer p1, * p2 = the first address of the array, * * p2 = the first value 1 of the array. For another example, there is a variable x with a value of 57. p1 points to this variable and p2 points to p1 variable, which can be simply represented by the following diagram:

Namely

int x = 57;
int * p1 = &a;
int ** p2 = &p1

4. On the difference between * int *p[n] and int (p)[n] in two-dimensional array

int *p[n] analysis: (pointer array)
[] has a higher priority than *, so int *p[n] is equivalent to int (p[n]), which is much clearer. Further evolution is (int *)(p[n]), which is complete. Obviously, P is an array whose data type is (int *), that is, n integer addresses (int) are elements, and the array name is p;

**int (*p)[n] * * analysis: (array pointer)
() and [] have the same priority, but the symbol is from left to right, so it can be written as (int) (* P) [n]). It can be considered that P is a pointer to n elements (array). There are n integer elements in the array. According to the knowledge of the array, * P is the address of the first element of the array, so p is also a double pointer here. Here, P can be considered as the array name of a double array;

More different can be referred to https://blog.csdn.net/wuyuzun/article/details/82778553

5. Copy it directly

int i;                             i It's an integer.
int *i;                           i Is an integer pointer.
int **i;                        i Is a pointer to an integer pointer.
int *i[5];                      i Is an array of integer pointers containing five elements.
int (*i)[5];          i Is a pointer to five elements.
int *i();                        i Is a function whose return value is an integer pointer. Pointer function
int(*i)();                      i Is a function pointer that returns an integer. Function pointer
int *(*i)();          i Is a function pointer, which returns an integer pointer.
int *(*i[])();                i Is an array of function pointers, which return integer pointers.
int *((*i)())[5]             i Is a function pointer. The function returns a pointer to five integer pointers.

6. Pointer to two-dimensional array

about int **p For example, p The object referred to is a pointer, then++p Just let p The value of increases the size of a pointer. Although the object referred to by the pointer can be large or small, the size of the pointer itself is certain. The current system is generally 4 bytes (or 8 bytes). That is, yes char *p and long *q For example, p and q It accounts for the same amount of memory (although char and long (different memory).
A typical mistake:
int a[2][3];
int **p = a;//error
 What's wrong with this code is a Is an array, and its members are also arrays, so a Called "array of arrays"—— C++Strictly speaking, there is no two-dimensional array in. Then, use a pointer to record a,You need to use a pointer that can represent "array of arrays". The following code is correct:
int (*p)[3] = a;//correct
 Only in this way can we guarantee++p send p point a The next pair of images (the pair is an array).
By the way: don't write“ int *p[3];"

7. new a dynamic two-dimensional array and realize memory continuity (refer to here) https://blog.csdn.net/u013534071/article/details/50877336 )

#include <iostream>
using namespace std;
 
int main()
{
	int **p;//Create a new secondary pointer p, that is, the pointer to the pointer
	p = new int*[3];//Open up three spaces [for storing pointers (i.e. addresses)], {p[0] p[1] p[2]}, where p → p[0]
	*p = new int[12];//Open up 12 spaces [for storage and shaping], {1 (2) (3) (4) (5) (7) (8) (9) (10) (12)} where p[0] → 1
	p[1] = p[0] + 4;//Let p[1] → 5 
	p[2] = p[1] + 4;//Let p[1] → ⑼ 
	int i, j, k = 1;
	int *q;//Create a new pointer q to make q move instead of p. p must keep its position unchanged to release memory later
	q = p[0];//The pointer q is assigned the content of p[0], i.e. Q → ⑴
	for (i = 0; i<12; ++i){//Since the memory is continuous, move the pointer through q + + to assign [from 1 to 12]
		*q = k;
		q++;
		k++;
 
	}
	for (i = 0; i<3; ++i){
		for (j = 0; j<4; ++j){
			cout << p[i][j] << endl;//p[i][j]=(p[i])[j], take p[i] as a whole, and "[]" can be regarded as the value symbol "*"
		}//That is, p[0][0]=**p, and so on
	}
 
	delete[] p[0];//Or delete[] *p
	delete[] p;//delete p[0] before delete p
 
	return 0;
}

The above creates a new secondary pointer through int**p, and opens up three spaces for storing pointers, that is, let p point to a pointer array. At this time, the value of p, that is, the address of the first element of the pointer array, obtains the value of the first element (int *), that is, another address through * p, and then through * p= new int[12], creates a 3 * 4 space in the space pointed to by the value of the first element, As shown in the following figure: (in order to represent two dimensions, int[12] is written as int[3][4], which is actually continuous in memory in this space)

Then, in order to ensure the continuity of addresses, instead of creating a new space in each pointer array, let the address in the pointer array point to the space just created by new, that is, let p[1] and p[2] point to somewhere in the space just created by new, so as to realize the continuity of the whole address: as shown in the following figure:

Topics: C++ pointer