16.5 null pointer
Remember how you can specify a character or string as null? If you don't remember, look here. A null character in a string represents the end of a string, but if a pointer is assigned to a null pointer, it does not point to anything. Null pointers often end with 0 or null. Null pointers are often used in and/or conditions in logical operations#include <iostream.h>
int main()
{
int x = 12345;
int* px = &x;
while (px) {
cout << "指针 px 指向了某些东西\n";
px = 0;
}
cout << "指针 px 指向了没有说明的 null!\n";
return 0;
}
If the pointer px is not NULL, it points to something, but if the pointer is null, it does not point to anything. A null pointer is useful when you have to test the state of the pointer to determine if it has a value.
16.6 Dynamic Memory allocation
You may be wondering how the programmer can allocate memory efficiently without knowing how much memory is needed before running the program. This is the interesting beginning of dynamic memory allocation.
Before the chapters, we learned to specify a pointer with the "address of" operator because it returns a variable or constant in memory in the form of a pointer. Now, the "address of" operator is not the only operator you can use to specify a pointer. In C + + You can also use another operator to return the pointer, which is the new operator. The new operator allows the program
A member allocates memory for a particular data, structure, class, and so on, and returns the address of the allocated memory as a pointer. The new operator is used as a right value, similar to the "address of" operator. See how the new operator works in the following code:
int n = 10;
SOMETYPE *parray, *pS;
int *pint;
parray = new SOMETYPE[n];
pS = new SOMETYPE;
pint = new int;
By specifying a pointer to an allocated memory segment instead of using a variable definition, you are essentially no longer a "middleman" (variable definition). Now, you can dynamically allocate memory without having to know the number of variables you should define. If you look at the code snippet above, you can also use the new operator to allocate memory to an array, which is quite handy when we want to maintain large arrays or class sizes. Because the new operator's pointer to the memory can also be "released", not destroyed but from your pointer to release. The delete operator is used in front of the pointer and releases the memory address that the pointer refers to:
delete parray;
delete pint;
The memory pointed to by Parray and Pint is released, which is good for maintaining large arrays and avoiding memory leaks. The amount of memory that is allocated needs to be properly released or it will cause a memory leak and your program will not run efficiently. Basically, every time you use the new operator, you should exit with the delete operator to free the memory. The delete operator, however, can be used not only to delete a pointer assigned by the new operator, but also to "delete" a null pointer, which prevents attempts to delete unallocated memory (This behavior can be compiled). The new and delete operators do not have to be used in conjunction with the same function or block of code. You can usually allocate memory in one function and free memory in another function. adjourned