A general technique for using pointers
Incorrect use of pointers, when assigning a value to a bad pointer, writes the data to an area of memory that should not be written to the value. This is called memory corruption and most of the effort to correct a pointer error is to find its location.
Using pointers correctly requires programmers to adopt a two-way strategy. First, avoid causing a pointer error first. Pointer errors are hard to spot, so it's worthwhile to take some preventative measures, and secondly, find out the errors as soon as possible after writing the code
Two ways to find the wrong way
1 limit pointer operations to subroutines or classes
2 declaring and defining pointers at the same time
3 checking the pointer before using the pointer
4 Check the variables referenced by the pointer before using it
5 Detecting damaged memory with a dog tag field ("Tagged field" or "Dog Card") is a field that you add to the structure to detect errors only. When assigning a variable, place a value that should remain constant in its tag field)
6 increased apparent redundancy (Operation Limited in subroutines)
7 using additional pointer variables to improve code clarity
8 simplify the complex pointer expression, you can draw a graph
9 Remove pointers in the linked list in the correct order
10 allocating a reserved memory fallback area
11 Crushing garbage data
12 set them to null after deleting or releasing pointers
13 check for illegal pointers before deleting variables
14 Tracking pointer Assignments
Three-table Drive method
The use of table-checking method can greatly simplify the program, making the program logic simple, intuitive to understand
Four differences for C + +, pointers (*) and references (&)
The most important difference is that the reference must always refer to an object, and the pointer can point to a null value, and the object pointed to by the reference cannot be changed after the reference is initialized; Use the pointer for the pass by reference parameter, use the const reference for the pass by value argument, C + + The default way to pass parameters to a subroutine is to pass a value instead of passing a reference; For non-modifiable object member references using A.A, use a->a; for object members that can be modified
Five when to use foreach
The Foreach loop or its equivalents are useful for performing operations on elements of an array or other container
Six ideas that should be avoided
1 the shorter the code, the faster the machine runs and the less resources it consumes
2 Do not think that the specific operation may be faster than others, the code size will be small, to actually test
3 no need to optimize anytime, anywhere
4 program running speed is efficient to ensure its correctness
Code Daquan reading notes (c)