[C++] 3-1.5 c++ null pointer

1. Operating environment

Operating system: IDE
used by windows10 : visual studio comunity 2019
Compiler standard: ISO C++17 standard (std:c++17)

2. Null pointer

In C++03, the null pointer is represented by "0". 0 is both a constant integer and a constant null pointer. It will appear: 0 is ambiguous;
in C language, the null pointer is represented by (void *)0;
in C++, "NULL" is used to represent the null pointer (VS internal implementation is #define NULL 0)

As shown in Figure 1: The
Insert picture description here
C++ Standardization Committee hopes that the "null pointer" is a certain thing.
That is, the reserved word "nullptr" is introduced as a null pointer in C++11.

As shown in Figure 2:
Insert picture description here
Note:
In order to avoid the problem of ambiguity, it is recommended to use the reserved word "nullptr" as a null pointer;
in the variable declaration, if there is no exact address can be assigned, assigning a nullptr to the pointer variable is a good programming Get used to avoiding wild pointers.

3. Example

-3.1 Judge the null pointer
Generally speaking, the program of the operating system is not allowed to access the memory at address 0, because the memory is reserved by the operating system.
Use this feature to determine the null pointer: the address of the pointer is 0, indicating that the pointer does not point to anything, that is, a null pointer.

The following example prints the value of the null pointer to determine whether it is a null pointer:

#include <iostream>

int main()
{
    
    
    //指针创建的时候,记得初始化,避免产生野指针。
	int* ptr1 = NULL;
	int* ptr2 = nullptr;
	std::cout << "ptr1 的值是 " << ptr1 <<std::endl;
	std::cout << "ptr2 的值是 " << ptr2 << std::endl;
		
	if (!ptr2)    /* 如果 ptr 为空,则为真 */
	{
    
    
		std::cout <<"打印逻辑判断:"  << (ptr1 == 0) << std::endl;
		std::cout << "打印逻辑判断:" << (ptr2 == 0) << std::endl;
		std::cout << "打印逻辑判断:" << (ptr1 == ptr2) << std::endl;
	}
	return 0;
}

Run as shown in Figure 3:
Insert picture description here

-3.2 Accessing the address of a non-null pointer
If the pointer p points to the memory address of an initialized variable a,
we can normally access the address value stored in the pointer p and the value stored in the memory address it points to through p and *p.

If we assign nullptr to the pointer p, print the address value stored in the pointer variable p and find that it is 0,
and trying to access the value stored in the memory address (0) pointed to by the pointer variable p will directly report an error!

In the following example, an error is reported directly when accessing a null pointer:

#include <iostream>
using namespace std;

int main(int argc, char const* argv[])
{
    
    
    int a = 537;
    int* p = &a;
    std::cout << p << ',' << *p << endl;

    p = nullptr;               //建议不要使用NULL
    std::cout << p << ',' << *p << std::endl;
    return 0;
}

Run as shown in Figure 4:
Insert picture description here

4. About wild pointer

A wild pointer is not a null pointer, but a pointer to garbage memory.
Reason: The pointer variable has not been initialized.
Any pointer variable will not be automatically initialized to a nullptr pointer when it is just created, and its default value is random.

Therefore, the pointer variable should be initialized when it is created. Either set the pointer to nullptr or let it point to legal memory. E.g:

//将指针设置为nullptr
int* p1 = nullptr ;
 
//或者指向合法的内存
int a = 537;
int* p2 = &a;

Guess you like

Origin blog.csdn.net/jn10010537/article/details/115310214