(Turn) C++ pointer problem

Original address: https://www.baidu.com/home/news/data/newspage?nid=8839758829366707907&n_type=0&p_from=1&dtype=-1

1 Avoid memory leaks

In c/c++, if it is memory on the stack (such as local non-static variables in functions), after use, the operating system will automatically reclaim it for us; but if it is dynamically allocated memory on the heap, We need to release manually.

If we forget to release these dynamic memory in the program, and the program is a service process that will continue to run, the memory usage will become higher and higher.

In a word, the memory that is no longer used is not released, it is called memory leak, and the problem of memory leak is very serious. Well, let's look at a few cases of memory leaks.

In C/C++, the dynamic memory allocated by the dynamic memory allocation function or the new operator needs to be released manually after use. Otherwise, memory leaks will occur.
write picture description here

Suggestion: Pay attention to malloc/free, new/delete in pairs when writing code

Even if free/delete is called to release memory after malloc/new, the free/delete statement that releases memory may not be executed due to exceptions, and memory leaks will occur. The following example is the case.
write picture description here
From the running result, the destructor of the class has not been executed, so it can be inferred that the delete statement has not been executed.

Some people will say, this is not easy, just add a delete t after the catch statement cout << "Something has gone wrong" << endl;

Suggestion: Pay more attention to the use of smart pointers in C++ code

2 Do not use wild pointers

Wild pointers, also known as dangling pointers, are pointers to "garbage" memory. Using "wild pointers" will cause programs to behave indeterminately.

Note that the wild pointer is not a NULL pointer, it is more prone to error than a NULL pointer, because it cannot be prevented by a judgment statement in the form of if (NULL == p), and we can only pay more attention when writing code.

After the pointer p is free or deleted, it is not set to NULL, which makes people mistakenly think that p is a legal pointer. In fact, free or delete only releases the memory pointed to by the pointer, but the value of the pointer is still the address of this memory. It's just that this memory has been reclaimed and cannot be used again by the process. The following example is a typical case of using wild pointers.
write picture description here
Suggestion: set the corresponding pointer to NULL after free or delete

Forgot to initialize when creating pointer variable p. The value of p is a random garbage value. At this time, it is dangerous to read and write the pointer, and the program will have uncertain behavior.
write picture description here
Suggestion : try to initialize the pointer variable when defining it, even if it is initialized as NULL is fine

In c/c++, local variables are stored in the stack, and their characteristics are that they are created when the function is called and destroyed when the function ends. Therefore, after returning the address of the local variable in the program, assign it to a pointer, which points to the A memory that has been reclaimed, which is also a wild pointer.

Take a look at the following example. Originally, I wanted to return the address of the variable i in the fun function to p, and use p to access this variable. This prints out that *p is 32767, not the value of variable i. 8. Bugs like this are difficult to locate once they appear in a large project.
write picture description here
Suggestion: Do not return the address of a local variable in a function. If the logic of the code is not the address of a local variable, then the local variable must be declared as static type, because the lifetime of a static variable is the duration of the entire program running.

3 Do not use NULL pointers

We all know that NULL pointers cannot be used in programs, but if you don't pay attention, programs may use NULL pointers unexpectedly. Let's look at two examples that are prone to problems.

When the dynamic memory allocation function allocates memory, it may fail to allocate memory. At this time, it returns NULL.
write picture description here
From the result of the program running, malloc allocation failure returns NULL to assign to p, and then accesses the content of the 0-address memory pointed to by p. "Segmentation fault" error.

Suggestion: When using the memory allocation function to allocate memory, you should use if(p==NULL) or if(p!=NULL) for error-proofing.

In addition, in a function with pointer parameters, it is also possible to use a NULL pointer by mistake. When calling the function, the pointer passed is a null pointer. If there is no if(p!=NULL) judgment condition, then use the pointer later. When the trouble is big, the following example is the case.
write picture description here
Suggestion: For functions with pointer parameters, you should also use if(p==NULL) or if(p!=NULL) for error proofing at the function entry.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325638279&siteId=291194637