Talking about the pointer array out of bounds in the c/c++ interview-wild pointer null pointer-pointer array pointer problem

Definition of pointer

  A pointer is an object in a programming language. Using an address, its value directly points to a value stored in another place in the computer memory. Simply put, every byte in the computer memory has a number, and the pointer stores this number, and a unique memory unit can be found. We usually write *p defined in the code. This is not a pointer in the real sense, but a pointer variable.

Confusing problem

  1. A pointer is subtracted from a pointer, and the result is not the difference in bytes, but the difference in the number of the same type, that is, the difference in bytes is divided by the type of the pointer.
  2. The size of the pointer and the pointer plus i are understood. The pointer is 4 bytes in a 32-bit computer, and it can be 4 bytes or 8 bytes in a 64-bit computer. It depends on the situation. The pointer stores the address, one word The maximum range of energy saving expression is 0~255, which is far from enough for computers. Therefore, 4 bytes are required, one byte is 2 ^ 8, 4 bytes is 2 ^ 32, and for Pointer plus i, the pointer represents the first address of the first element in this group of data, when i is equal to 1, the data plus the address of the corresponding type.

Instance

Insert picture description here
Code

#include <stdio.h>
int main()
{
	int i = 0;
	int arr[10] = { 0 };
	int* a = arr;
	for (i = 0; i <= 12; i++)
	{
		*(a+i) = 0;
		printf("sb %d", i);
	}

	return 0;
}

  Simply analyze the code, create an array of type int, the array can store 10 data, the byte size is 40, because the first element is initialized to 0, the latter is not initialized, so the following elements default to 0, and then created An int type pointer, which stores the address of the first element under the array arr. The for loop loops 13 times. Obviously this is beyond the range of the array, and the elements in the array are re-assigned to 0 in the loop , And print out the values ​​of sb and i. Careful people will find that the function obviously loops 12 times, but 12 has not been output during the printing process. Afterwards, after running, it will be found that this code first does not report an error, and it is an infinite loop. This is because the system conducts random checks on the out-of-bounds of the array. Not all out-of-bounds compilers can find out. For the problem of infinite loops, I check the address of the first element of arr through debugging. The
Insert picture description here
  address 0x00D5FC68 is the address of the first element of arr, and the address 0x00D5FC98 is the address of i. Because the array is out of bounds, the value of i is re-assigned to 0 in the 12th loop, which causes the loop to restart At the beginning, repeat this form an endless loop.

Wild pointer

  Officially, they are uninitialized pointers and pointers that are out of bounds of the array. In layman's terms, you don't know where the pointer points. The compiler can often get the value inside when it is used, but when the program ends to release the resources, because this part of the resource does not originally belong to it, so the program will have problems.

Null pointer

  A pointer that does not point to any object or function. During initialization, assignment, or comparison, if one side is a pointer type value or expression, the compiler can determine that the constant 0 on the other side is a null pointer and generate the correct null pointer value.

char *p = 0;
if (p != 0)

Analysis of Wild Pointer and Null Pointer

  A wild pointer is a pointer to any location, and a null pointer ensures that it does not point to any object or function. The declaration methods and sources of the two are also different, and reasonable use of null pointers can avoid memory leaks.

Analysis of Pointer Array and Array Pointer

  The pointer array and the array pointer are completely different. The pointer array is still an array in essence, but the data stored in the array is of pointer type, and the array pointer is essentially a pointer, which is a pointer to an array. The definition methods of the two are also different.

//指针数组
int* arr1[10];
//数组指针
int  arr2[10];
int (*p2)[10] = &arr2;

  The initial values ​​defined by these two methods are the same, but when adding 1 to the two at the same time, the pointer array changes 1 byte, but the array pointer needs to change 4 bytes.

Guess you like

Origin blog.csdn.net/weixin_43580319/article/details/111196008