Preface: The purpose of this article is to document the easy-to-ignore details in the C language. I plan to take a little time every day to read and tidy, persist, today is the first, perhaps next month's today is the second one, next year's Today is the third? ...... I firmly believe that a good memory is inferior to bad writing.
First link: Easy to ignore in C language details (first article)
1. there is only one-dimensional array in the C language, and the size of the array must be determined as a constant at compile time. The array element in the C language can be any object, or it can be another array, an array of arrays.
2. The C language allows the initialization list to appear with extra commas. For example: int days[] = {n/A,}; function: facilitates automated generation of code.
3, because A[i] and * (A + i) are equivalent, and because A+i and i+a the same meaning, so a[i] and I[a] have the same meaning.
4 . In an expression that uses the operator "&&", place the subexpression that is most likely to be false to the left of "&&", using the operator ' | | ' expression, place the sub-expression that is most likely to be true in the ' | | ' Left. Because of the " sudden death" approach to the logical expression, C + +: If the subexpression on the left side of "&&" evaluates to False, the entire expression is false, and the subsequent subexpression does not need to be evaluated; The left sub-expression evaluates to true, and the entire expression is true, and subsequent sub-expressions do not have to be evaluated. This method can improve the efficiency of the program.
5, for multidimensional arrays, the correct way to traverse is to see in what order the language to arrange the storage space of the array elements. the "First after column" Traversal efficiency is certainly better than the " row by row" traversal, regardless of whether the number of rows is greater than the number of columns or the opposite or even closer to the multidimensional array of C/s + +. The efficiency of the main effect is mainly caused by large arrays of memory page exchange and cache hit rate, rather than the number of cycles itself.
6 . Why is the fgetc () function return value int instead of unsigned char?
When a function call is faulted or the end of the file is read, fgetc () returns EOF, which is-1. If EOF is saved in the unsigned char type, the result is 0xFF, and 0xFF is a byte of ASCII code meaning, so it will not be possible to distinguish between byte 0xFF or Terminator EOF. If EOF is saved in the int type, the result is 0xffffffff, and if read to byte 0xFF, the conversion to the int type is 0X000000FF, and the EOF and byte 0xFF can be correctly distinguished.
7 . If the function has no arguments, void should be used instead of empty, because standard C interprets the empty argument list as acceptable for any type and number of arguments, whereas standard C + + interprets the empty argument list as unacceptable. In particular, you should be aware of this difference when porting C + + programs.
8, at the "exit" of the function body, check the correctness and efficiency of the return statement.
(1)The return statement cannot return a "pointer" or "reference" to "stack memory" because the memory unit is automatically freed at the end of the function body.
There is a hidden danger of the notation char *func (void) { char str[] = "Hello World"; return str; The statement has a hidden danger, the memory unit that STR points to will be freed } //workable notation const char *func (void) { const char *p = "Hello World"; return p; Returns the address of a string constant }
(2) if the function return value is an object, consider the efficiency of the return statement.
return string (S1 + s2); Statement 1string result (S1 + s2);
Statement 1 is not equivalent to statement 2. Statement 1 represents the creation of a temporary object and returns it. Statement 2 performs three things:
1)The result object is created, invoking the corresponding constructor initialization;
2) call the copy constructor and copy result to the external storage unit that holds the return value.
3)result call destructor is destroyed at the end of the function.
Note: create a temporary object and return it, the compiler can directly create and initialize the temporary object in the external storage unit, eliminating the cost of copying and destruction, improve efficiency. The intrinsic data type variable does not have constructors and destructors, takes the creation of temporary variables and returns, does not improve much efficiency, but makes the program more concise. For example:
int x, Y;return x + y;
9, do not omit the type of the return value, if the function does not return a value, it should be declared as void type. In standard C, all functions that do not add type descriptions are automatically treated as int types. There is no benefit in doing so, but it is easily misunderstood to return void type. The C + + language has strict static type security checks and does not allow this to happen. Since C + + programs can call the functions of the function, in order to avoid confusion, we stipulate that any C + + function must have a return value type.
10, if the parameter is a pointer, and only for input purposes, you should add a const before the type to prevent the memory cell pointed to by the pointer from inadvertently being modified in the function body. If the input parameter passes the object as a value, it is passed in a "Const &" manner, because the creation and destruction of the reference does not invoke the object's construction and destructor, thereby increasing efficiency.
The easy-to-ignore details in C language (second article)