[C traps and defects] semantic Defects

Source: Internet
Author: User

I. pointers and Arrays
1) in C language, there is only one-dimensional array, and the size of the array must be determined as a constant during compilation.
2) For an array, we can only do two things: determine the size of the array and obtain the pointer to the element whose subscript is 0.
Example 1:
Int a [3];
The result of sizeof (a) = 12 is the size of the entire array a, rather than the size of the pointer to the element of Data.
Int calendar [12] [30];
Sizeof (calender [4]) = 120 the size of 31 sizeof (int) Values
Calendar [1] [2] is equivalent to * (calendar + 4) + 7 ).

Example 2:

Int * p. The unit is the size of an int.
For int * p [31], the size of 31 int values is
Int calendar [12] [31];
Int ** p = calendar; error because the unit is different
Int (* p) [31]; p = calendar; correct, in the same unit
But for strings:
Char calendar [12] [31];
Char ** a = calendar;
Char * a [] is equivalent to char **
Correct, because each operation string, that is, printf ("% s", a [0]), does not involve operations such as a [1] [2, therefore, the Unit of each operation is char *

Example 3: the address of the actually nonexistent 'barri' element in the array is located after the memory occupied by the array. This address can be used for value assignment and comparison. Of course, if you want to reference this element, that is illegal.
# Include <stdio. h>
Int main ()
{
Int calendar [12] [31];
Int (* monthp) [31];
For (monthp = calendar; monthp <& calendar [12]; ++ monthp ){
Int * dayp;
For (dayp = * monthp; dayp <& (* monthp) [31]; ++ dayp)
* Dayp = 0;
}
}

2. Use of non-array pointers
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Int main ()
{
Char * r;
Char * s = "hi ";
Char * t = "man ";
R = malloc (strlen (s) + strlen (t) + 1); // includes the final '/0'. strlen does not include'/0'
If (! R ){
Exit (1 );
}
Strcpy (r, s );
Strcat (r, t );
Printf ("% s \ n", r );
Free (r );
}
Note: malloc may not properly allocate memory, so check whether the allocation is successful.
After using the pointer, remember to release it. Free (char *)
A null pointer is not a null string and cannot be referenced.

3. Uncertainty of the order of Value
Y [I] = x [I ++] cannot determine when I ++ will be used
Change to y [I] = x [I]; I ++

4. Integer Overflow
In an unsigned arithmetic operation, there is no overflow. Only when both operands are signed integers can overflow occur and the overflow result is undefined.
INT_MAX is a defined constant that represents the maximum possible integer. As defined in <limits. h>, you can use the following method to determine whether overflow exists.
Changing the symbol of a positive integer can ensure that it does not overflow. The only problem is that the symbol of a negative number is changed. Therefore, if we can
Do not convert the minimum negative number to an integer.
Two methods:
(1) Forcibly convert both a and B to unsigned integers.
If (unsigned) a + (unsigned) B> INT_MAX)
ERR
(2) directly use the maximum value
If (a> INT_MAX-B)
ERR

V. Efficient binary classification
(1) pointer operations are faster than Array Operations
(2) shift operations are faster than Division operations
# Include <stdio. h>

Int * bsearch (int * t, int n, int x ){
Int * lo = t, * hi = t + n-1;
While (lo Int * mid = lo + (hi-lo)> 1 );
// It cannot be int * mid = (hi + lo)/2 because it is wrong to add the pointer,
// Only the pointer can be iterated, that is, ++ or --, or subtract the calculation distance.
If (x <* mid ){
Hi = mid-1;
} Else if (x> * mid ){
Lo = mid + 1;
} Else {
Return mid;
}
}
Return NULL;
}
Int main ()
{
Int a [] = {1, 2, 4 };
Printf ("% d", * bsearch (a, 4, 2 ));
}

Vi. array value Boundary
(1) The value range is the difference between the upper and lower bounds.
(2) If the value range is null, the upper bound is equal to the next
Void bufwrite (char * p, int n)
{
While (-- n> = 0)
{
If (bufptr ==& buffer [N])
{
Flush ();
}
* Bufptr ++ = * p ++;
}
}
When the bufptr and & buffer [0] are equal, the content in the buffer zone is empty.
The number of characters stored at any time is bufptr-buffer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.