High Quality C ++C Program Design Guide Abstract (2) (original)

zhaozj2021-02-12  139

Good advice:

1. After the variable (pointer and array) is created, they should be initialized in time to prevent the uninitialized variables from being used as the right value.

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

3. Beware Beware of data type conversion errors, try to use explicit data type conversion to avoid implicit data type conversions to the compiler.

4. Beware of underflow or overflow, the subscript of the array.

5. Beware of the file I / O error.

6. Beware of "error handler", or "error handler" itself has an error.

7. Try not to use the variables close to specific hardware or software environment.

Quote: 1. For example INT M; int & n = m; then n is a reference to M.

2. Reference rules: 1 The reference must be initialized while being created. (The pointer can be initially at any time

Chemize

2 Do not have NULL references, reference must be associated with legal storage units (pointer can

NULL)

3 Once the reference is initialized, the reference is not changed. (The pointer can be at any time

Change the object referred to)

Parameters of the function:

1 The writing of the parameters should be complete, and the parameter name cannot be omitted only.

2 Parameter naming is easy to understand, the order is reasonable, the general purpose parameters are before, the source parameters are behind.

3 If the parameter is a pointer, and only input is used, CONST should be added before the type to prevent the pointer from being repaired in the function.

change.

4 If the input parameter passes the object in the way, the "const &" mode is applied, and the temporary object can be saved.

Construction and secting process, improve efficiency.

return value:

1 Do not omit the return value type. If there is no return value, it should be declared as a Void type.

2 Normal output is obtained by the output parameters, and the error flag returns with the return statement.

3RETURN statement does not return a pointer or reference pointing to "Stack memory", because the internal existence is automatically pin

destroy.

Assert:

It is only a macro that works only in the Debug version, which is used to check the case that should not occur. When writing a function, it is necessary to review and ask "What assumptions I am planning", once the assumption is determined, it is necessary to check the assumptions using the assertion.

Memory allocation:

1 Assign from a static storage area. When the program is compiled, it has been allocated, and the entire program is existed.

Existence during operation, such as global variables, static variables.

2 Create on the stack. The storage unit of local variables within the function can be created on the stack, and the function is automatically released at the end.

3 allocate from the heap. Also known as dynamic allocation of memory, with malloc / free or new / delete.

Memory error:

(1) The memory allocation is unsuccessful, but it uses it. So check if the pointer is NULL before using the memory.

(2) Memory allocation is successful, but not initialized. Therefore, the assigned memory must be initialized, and the number is set to zero.

(3) Memory allocation is successful and initialized, but operating the crossing. (4) Forget to release memory, resulting in "memory leakage". So malloc / free or new / delete is used.

(5) Memory release is still used. The reason may be: Return statement points to the stack memory pointer is returned; after the memory is released, the pointer is not set to NULL, resulting in the wild pointer.

Pointers and arrays:

(1) Arch Either creation in a static storage area, or create, addresses, and capacity on the stack, the content varies. The pointer can point to any memory block. Such as: char a [] = "Hello"; a [0] = 'x'; // correct char * p = "world";

P [0] = 'x'; // error, because "world" is a constant string

Char a [] = "Hello World";

Char * p = a;

SIZEOF (A) = 12; // 11 '/ 0' = 12

SIZEOF (P) = 4; // sizeof (char *) = 4;

(2) When the array is transmitted as a function of the function, the array automatically degenerates into the same type of pointer. Void Func (Char A [100])

{

SizeOf (a); // sizeof (a) = 4

}

The pointer as the parameter of the function, cannot be expected to apply for dynamic memory with this pointer. Such as:

Void test (void)

{

CHAR * STR = NULL;

GetMemory (STR, 100);

}

Void getMemory (char * p, int num) {

P = (char *) malloc (sizeof (char) * NUM);

}

Str = NULL;

A pointer to the pointer can be used.

Void test (void)

{

CHAR * STR = NULL;

GetMemory (& STR, 100);

Free (STR);

}

Void getMemory (char ** p, int Num)

{

* p = (char *) malloc (sizeof (char) * NUM);

}

or

Void test (void)

{

CHAR * STR = NULL;

Str = getMemory (100);

Free (STR);

}

Char * getMemory (int Num)

{

Char * p = (char *) malloc (sizeof (char) * NUM);

Return P;

}

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

New Post(0)