"High Quality C ++ Programming Guide" Reading Notes (2)

zhaozj2021-02-17  138

Memory management:

After using the Malloc or New to apply for memory, you should immediately check if the pointer value is NULL. Prevent memory using the pointer value of NULL.

Don't forget to pay initial values ​​for array and dynamic. Prevent memory that will not be initialized as the right value.

There is a group or the subscript of the pointer, especially "more 1" or "less 1" operation occurs.

The application and release of dynamic memory must be paired to prevent memory leakage.

After released the memory with free or delete, set the pointer to NULL to prevent "wild pointer".

Advanced features of C functions:

The default value of the parameter can only appear in the declaration of the function, and cannot appear in the definition body.

If the function has multiple parameters, the parameters can only be deficed from the back, otherwise the function calls statement blame.

Class constructor, destructor and assignment function:

Many difficult program errors are due to the correct initialization or clearance of variables, while initialization and clearing work are easily forgotten.

Use rules for constructor initialization table:

If there is a inheritance relationship, the derived class must call the constructor of the base class in its initialization table.

The CONST constant of the class can only be initialized in the initialization table because it cannot be initialized in the functional body.

The initialization of the data member of the class can be used in both initialization tables or functions in the function, and the efficiency of both ways is not exactly the same.

The construction function, destructuring function, and assignment function of the base class cannot be inherited by the derived class. If there is a inheritance relationship between the class, you should pay attention to the following when writing the above basic functions:

The constructor of the derived class should call the constructor of the base class in its initialization table.

The destructor of the base class and the derived class should be the virtual (ie, the Virtual keyword).

When writing the assignment function of the derived class, be careful not to forget the data member of the base class to re-value.

Inheritance and combination of classes:

If class A and class B are not associated, it is not possible to let B inherit A function and properties in order to make B function more. Don't think "White White Don't eat", let a good-end healthy youth to eat people to eat people.

If it is logically b is A "a Kind of), the function and attributes for inheritance A is allowed.

If a part of the B is B is logically A, B is not allowed to be born from A, but to combine B using A and other things.

Other programming experience:

Don't pursue the efficiency of the program, you should improve the efficiency of the program under the premise of satisfying the correctness, reliability, robustness, readability.

The global efficiency of improved procedures is mainly, and local efficiency is improved.

When optimizing the efficiency of the program, you should first identify the "bottleneck" of the restriction efficiency, do not optimize the insignificant.

Optimize the data structure and algorithm first, and then optimize the execution code.

Sometimes time efficiency and space efficiency may be opposite, at this time, it should analyze the more important and appropriate compromise. For example, spend some memory to improve performance.

Don't pursue compact code, because compact code does not produce efficient machine code.

Some beneficial recommendations:

Be careful to write errors when you visually can't distinguish.

We often write "==" to "=", like "||", "&&", "<=", ">", is also very prone to "loss 1" mistakes. However, the compiler does not necessarily automatically point out such an error.

Variables (pointers, arrays) should be initialized after being created to prevent the variables that are not initialized to be used as right values.

Beware of the initial value of the variable, the default value is incorrect, or the accuracy is not enough.

Beware of an error occurred. Try to use explicit data type conversion (let people know what happened), avoid making the compiler slidably implicitly converted.

"Beware of the cubic variables, the subscript of the array.

Be careful to write the wrong handler, when the heart error handler itself is incorrect.

Beware of the I / O error.

Avoid writing skills high.

Do not design the face, very flexible data structure.

If the original code quality is better, try to multiplex it. But don't patch a very bad code, you should rewrite.

Try to use the standard library function, do not "invent" existing library functions.

Try not to use a variable that is closely related to specific hardware or software environment.

Set the selection of the compiler to the most stringent state.

If possible, use PC-LINT, LOGISCOPE and other tools for code review.

转载请注明原文地址:https://www.9cbs.com/read-31420.html

New Post(0)