The book covers the following:
The excerpt is as follows:
P13: PortableCode
A standard-compliantProgramIt should be:
Use only identified features
Do not break through any restrictions implemented by the compiler
Does not generate any output dependent on features defined by the compiler or undefined
P31: in C, the const keyword does not really represent a constant, as shown in
Const int two = 2; Switch (I) {Case 1: XXX Case Two: xxx ...}
Error
P32: fall through caused by the absence of a break statement after the case tag. The default behavior of the switch statement is 97% errors.
When the operand of sizeof is a type name, parentheses must be added on both sides. However, if the operand is a variable, parentheses are not required. For example:
Sizeof asizeof (INT)
P68: typedef can be regarded as a complete encapsulation type. The difference between typedef and macro is reflected in two aspects:
1. The macro type name can be extended with other type specifiers, but the type name defined by typedef cannot.
# Define peach intunsigned Peach I; // correct typedef int banana; unsigned banana I; // Error
2. In the declaration of several consecutive variables, the type defined by typedef can ensure that all the variables in the Declaration are of the same type, while the type defined by # define cannot be guaranteed.
# Define int_ptr int * int_ptr chalk, cheese;
Change
Int * chalk, cheese; // chalk is an int pointer, and cheese is an int
This problem does not occur when typedef is used.
P87: when defining a pointer, the compiler does not allocate space for the object to which the pointer is directed. It only allocates space for the pointer itself, unless a String constant is assigned to the pointer during definition for initialization, but you cannot expect to allocate space for constants such as floating-point numbers.
Char * P = "breadfruit"; // correct float * Pip = 3.141; // Error
P93: if a copy of the function library is a physical component of an executable file, it is called a static link. If an executable file only contains a file name, let the loader find the function library required by the program at runtime, it is called dynamic link.
P102: interpositioning replaces the behavior of the library function by writing a function with the same name as the library function, but it is easy to replace the definition of a symbol in your code with the same symbol in the function. Not only will all your calls to this function be replaced by function calls of your own version, but all system calls to this library function will also be replaced by your function.
P127: to return a pointer to a variable defined in the function, declare the variable as static. This ensures that the variable is saved in the data segment rather than in the stack.
P153: malloc and free -- get the memory from the heap and return the memory to the heap.
BRK and sbrk -- adjust the data segment size to an absolute value
P158-159:
Bus errors: these errors are almost all caused by unaligned read or write. They are called Bus errors because the blocked component is the address bus when an unaligned memory access request occurs. For example
Union {char a [10]; int I;} U; int * P = (int *) & (U. A [1]); * P = 17; // an unaligned address in P may cause a bus error.
Segment error: Due to an exception in the memory management unit, this exception is usually caused by removing the reference of an uninitialized or invalid value pointer. For example:
Int * p = 0; * P = 17; // segment Error
P161: Causes of segment errors
1. Release a pointer that contains an invalid value.
2. unreference a null pointer
3. Access is performed if the correct permissions are not obtained.
4. The stack or heap space is used up.
Common Errors
1. Bad pointer error: use it to reference memory before pointer assignment
2. Rewrite error: write data beyond the array boundary, write data outside the two ends of the dynamic storage memory, and rewrite some heap management structures.
3. pointer release error: release the same memory twice, release a memory that has not been allocated with malloc, release the memory that is still in use, and release an invalid pointer.
P200: When the array and pointer are the same
Array declaration:
Extern, such as extern char a []; cannot be rewritten as a pointer
Definition, such as char a [10]; cannot be rewritten as a pointer
Function parameters, such as func (char a []); you can choose array or pointer as you like.
The array is used in the expression:
For example, c = A [I]; you can select the array form or pointer form as you like.
P201: When is the array and pointer equivalent:
1. the array name in the expression is treated as a pointer to the first element of the array by the compiler.
2. The subscript is always the same as the pointer offset.
3. In the declaration of function parameters, the array name is treated as a pointer to the first element of the array by the compiler.
P207: No way to pass the array itself to a function, because it is always automatically converted to a pointer to the array
P209: an aggregation of arrays and pointers:
1. Access to arrays in the form of a [I] is always accessed by pointers like * (a + I) written or interpreted by the compiler.
2. A pointer is always a pointer, which cannot be rewritten as an array.
3. in a specific context, that is, it acts as a function parameter. The declaration of an array can be considered as a pointer. The array as a function parameter will always be modified by the compiler as a pointer to the first element of the array.
4. When defining an array as a function parameter, you can define it as an array or a pointer with your choice. No matter which method you choose, what you actually get inside the function is a pointer.
5. Definitions and declarations must match in all other cases. If an array is defined, it must also be declared as an array when other files declare it, as is the case with pointers.
P225: the array and pointer parameters are modified by the compiler.
Real parameter -------------------------------------- the matching Parameter
Array char C [8] [10] ----------------- char (*) [10] array pointer
Pointer array char * C [15] ------------------- char ** C pointer
Array pointer char (* C) [64] ------------------- char (* C) [64] not changed
Pointer char ** C ---------------------- char ** C does not change
P227: Use a pointer to pass a multi-dimensional array to the Function
My_function (INT my_array [10] [20]) // my_function (INT my_array [] [20]) with a large limit // The rightmost one-dimensional length must be 20my_function (INT (* my_array) [20]) my_function (char ** my_array) // this can be done only when the two-dimensional array is changed to a pointer array pointing to the vector.
P234: the basic idea of creating a dynamic array is to use the malloc () library function to obtain a pointer pointing to a large block of memory, for example:
# Include <stdlib. h> # include <stdio. h> int size; char * dynamic; char input [10]; size = atoi (fgets (input, 7, stdin); Dynamic = (char *) malloc (size ); dynamic [0] = 'a'; Dynamic [size-1] = 'Z ';
P264: A simple subset of C ++
C ++ features:
1. Class
2. constructor and destructor, but only for simple examples of function bodies
3. overload, including operator overload and I/O
4. Single-inheritance and Polymorphism
Avoid using the C ++ features:
1. Template
2. Exceptions
3. virtual base class
4. Multi-Inheritance
P277: library function call and system call
Function library calls are part of a language or application, while system calls are part of an operating system. system calls are performed after the operating system kernel discovers a trap or an interruption. Library Function calls are generally slower than the Code expanded in the row because it requires additional overhead of function calls, but system calls are much slower than library function calls, because it needs to switch the Context Environment to the kernel mode.
P279: file descriptor and file pointer
All system calls to manipulate files accept a file descriptor as a parameter or return it as a return value.
The file descriptor is an offset of each input table of an open file. It is used in UNIX system calls to identify a file.
The file pointer stores the address of a file structure, which is used to represent open I/O streams. It is used to identify files in ansi c standard I/O library calls.
Summary:
This book has been taken back from seven for a long time, and seven said it was well written. Now I have finally read this book, and it is indeed a result. I have to say that the time for reading program books in my recent internship is not as long as last year. It is estimated that most of my work has been spent on reading idle books. I 'd like to watch more program books during this so-called summer vacation, continuous learning makes progress.