Question 1:
Void test1 () { Char string [10]; Char * str1 = "0123456789 "; Strcpy (string, str1 ); } |
Question 2:
Void Test2 () { Char string [10], str1 [10]; Int I; For (I = 0; I <10; I ++) { Str1 [I] = 'a '; } Strcpy (string, str1 ); } |
Question 3:
Void test3 (char * str1) { Char string [10]; If (strlen (str1) <= 10) { Strcpy (string, str1 ); } } |
Answer:
Question 1 string str1 requires 11 bytes to be stored (including '\ 0' at the end), while string only has 10 bytes of space, strcpy will cause the array to cross-border;
For question 2, if the subject points out that the character array str1 cannot end in the array, it can give 3 points; if the subject points out strcpy (string, str1) calling makes the number of bytes replicated from the str1 memory to the string memory uncertain. It can be given 7 points. Based on this, it is pointed out that the strcpy function is working for 10 points;
For question 3, if (strlen (str1) <= 10) should be changed to If (strlen (str1) <10 ), the result of strlen does not count the 1 byte occupied by '\ 0.
Analysis:
Measure the test taker's knowledge about basic skills:
(1) The string ends with '\ 0;
(2) sensitivity to array out-of-bounds control;
(3) how the database function strcpy works. If the total score of a standard strcpy function is 10, the following are several different answers:
2 points
Void strcpy (char * strdest, char * strsrc) { While (* strdest ++ = * strsrc ++ )! = '\ 0 '); } |
4 points
Void strcpy (char * strdest, const char * strsrc) // Add the source string to const, indicating that it is an input parameter and adds 2 points { While (* strdest ++ = * strsrc ++ )! = '\ 0 '); } |
7 points
Void strcpy (char * strdest, const char * strsrc) { // Add non-0 assertions to the source and target addresses, and add 3 points Assert (strdest! = NULL) & (strsrc! = NULL )); While (* strdest ++ = * strsrc ++ )! = '\ 0 '); } |
10 points
| // For chained operation, add 3 points to return the destination address! Char * strcpy (char * strdest, const char * strsrc) { Assert (strdest! = NULL) & (strsrc! = NULL )); Char * address = strdest; While (* strdest ++ = * strsrc ++ )! = '\ 0 '); Return address; } |
We can clearly see from two to ten answers that the little strcpy has hidden so many xuanjicang! What a solid basic skill is required to write a perfect strcpy!
(4) Master strlen, which does not include '\ 0' at the end of the string '.
After reading strcpy versions with different scores, you can write a strlen function with 10 points. The perfect version is int strlen (const char * Str). // enter the const parameter.
{ Assert (strt! = NULL); // The asserted string address is not 0 Int Len; While (* STR ++ )! = '\ 0 ') { Len ++; } Return Len; } |
Question 4:
Void getmemory (char * P) { P = (char *) malloc (100 ); }Void test (void) { Char * STR = NULL; Getmemory (STR ); Strcpy (STR, "Hello World "); Printf (STR ); } |
Question 5:
Char * getmemory (void) { Char P [] = "Hello World "; Return P; }Void test (void) { Char * STR = NULL; STR = getmemory (); Printf (STR ); } |
Question 6:
Void getmemory (char ** P, int num) { * P = (char *) malloc (Num ); }Void test (void) { Char * STR = NULL; Getmemory (& STR, 100 ); Strcpy (STR, "hello "); Printf (STR ); } |
Question 7:
Void test (void) { Char * STR = (char *) malloc (100 ); Strcpy (STR, "hello "); Free (STR ); ... // Other omitted statements } |
Answer:
In question 4, the form parameter of the getmemory (char * P) function is a string pointer. Modifying the form parameter within the function does not actually change the value of the input form parameter.
Char * STR = NULL; Getmemory (STR ); |
The subsequent STR is still NULL;
Question 5
Char P [] = "Hello World "; Return P; |
The P [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.
Getmemory in question 6 avoids Question 4. The input parameter of getmemory is a pointer to the string pointer, but the request memory and value assignment statement are executed in getmemory.
| * P = (char *) malloc (Num ); |
If the memory application is successful, add:
If (* P = NULL) { ... // Handle the Memory Request failure } |
Question 7 has the same problem as question 6.
| Char * STR = (char *) malloc (100 ); |
In addition, after free (STR), STR is not set to null, which may become a "wild" pointer. The following should be added:
In the test function of question 6, the memory of malloc is not released.
Analysis:
Question 4 ~ 7. Measure the test taker's understanding about memory operations. Generally, 50-50 of the respondents with solid basic skills can answer these questions correctly ~ 60 error. However, it is not easy to answer the correct questions completely.
The memory operations are mainly focused on:
(1) pointer understanding;
(2) The survival time and scope of the variable;
(3) good dynamic memory application and release habits.
Let's take a look at the following program errors:
Swap (int * P1, int * P2) { Int * P; * P = * P1; * P1 = * P2; * P2 = * P; } |
In the swap function, P is a "wild" pointer, which may point to the system zone, causing program running to crash. In VC ++, the "access violation" error is prompted during debug ". The program should be changed:
Swap (int * P1, int * P2) { Int P; P = * P1; * P1 = * P2; * P2 = P; } |