Record some bugs that are easy to generate in C language, some are encountered in self-programming, and some are seen elsewhere:
1. "user breakpoint called from code at 0x ********"
This bug often occurs when the free function is called to release the memory of malloc. It was encountered many times before, but the cause of the problem has not been found.
I checked it on the Internet. Some of them are quite complicated and involve the content of the operating system push and maintenance. I haven't touched this part yet, so I didn't take it into detail. When it comes to repeated release of dynamic memory, modifying the dynamic memory pointer or destroying the dynamic memory ending mark of the system will cause this bug. Later, I checked my code and found that the problem of repeated release was not found, but there was a frequent operation in the memory beyond the dynamically applied memory, which led to this problem.
Problem summary: dynamic memory is often used improperly when it is released.
1. Release dynamic memory repeatedly. After free is called, the pointer should be assigned NULL. It is best to write a macro to include free and NULL pointer values to avoid omission;
2. The read/write operation exceeds the dynamically applied memory boundary (read should not be faulty, but avoid it unless you really understand what you are doing)
2. the string in C is abnormal.
Strictly speaking, C does not contain string variables. Generally, character arrays or character pointers are used to perform string operations, but it is not exactly equivalent. Pay attention to the differences between the following two sentences:
Char a [] = "Hello world"; // It is stored in the stack. You can modify any value of Data Group.
Char * s = "Hello world"; // the pointer s is stored in the stack, and The String constant is in the code segment and cannot be modified.
Assigning values to s [I] will cause errors.
3. Be sure to use the short-circuit feature to prevent exceptions when determining conditions, such
While (j> = 0 & a [j]> 0) j --;
If (d! = 0 & n/d = 0)
If (p = NULL | * p = '\ 0')/* no string */
In three cases, if the first condition is missing or the order of the two judgment conditions is changed, memory overflow will occur.
4. Chaotic Type Extension Problems
Because you are not sure which protection rules the compiler uses (unsigned protection or value protection), try to avoid mixing signed and unsigned variables in the same expression. Explicit type conversion can always be used to clearly express the desired conversion place or method at any time.
5. type conversion is treated as the left value.
In C language, a type conversion operation can only generate one right value, and cannot be assigned values or perform auto-increment (subtraction) operations.
For example
Char * p;
(Int *) p) ++;
You cannot add p to the int length as expected, but it should be as follows:
P = (char *) (int *) p + 1 );
Or p + = sizeof (int );
6. Will the function call of the variable length parameter convert the default type?
What is the output of the following code?
# Include <stdio. h>
Int main ()
{
_ Int64 n;
Int B;
N = 1;
B = 2;
Printf ("% d, % d \ n", n, B );
Return 0;
}
What is different from expectation is that it outputs: 1, 0
This is because in Variable Parameter Function calls, the compiler generally does not perform default type conversion, when a function extracts parameters from the stack based on the format string, an exception is generated because the variable length is inconsistent.
This article is from the blog "xuexiangjing ".