I haven't learned C language some days ago. When I was watching windows program design the other day, I found it in VC 6 according to the windows architecture.
The following code shows that your C language skills are really poor. The Code is the C code I see. So far, I can only understand it;
It is estimated that it will be easy to understand.
View Code
DECLARE_HANDLE (HDC); # define DECLARE_HANDLE (name) struct name ##__ {int unused;}; typedef struct name ##__ * name
The above code will be shared with you after I understand it. Let's talk about this C language interview question.
8. Locate the wrong question
Find out the following function errors:
Question 1:
Void test1 ()
{
Char string [10];
Char * str1 = "0123456789 ";
Strcpy (string, str1 );
}
/*
At first glance, there is no error. The two real parameter types passed to the strcpy () function can meet the requirements.
However, we will find that this function has an out-of-bounds problem. The string "0123456789"
The length is strlen ("0123456789") + 1 = 11, and obviously string [10], it is impossible to store such a large space.
When using the strcpy function, you must consider the size of the source string and the target string.
*/
Changing the function to the following form may be more robust:
Int StrCpy (const char * source; char dest [])
{
If (NULL = source | NULL = dest | (strlen (dest) <strlen (source )))
Return 1; // the return value is 1, indicating that the replication failed.
Else
Strcpy (dest, source );
Return 0; // return value = 0 indicates that the copy is successful.
}
Question 2 ::
Void test2 ()
{
Char string [10], str1 [10];
Int I;
For (I = 0; I <10; I ++)
{
Str1 = 'a ';
}
Strcpy (string, str1 );
}
/*
This question examines two questions:
1. The first address of the array is a constant and cannot be left. That is, str1 is a constant,
It represents the first address of the entire array.
2. subscript is required for referencing the second array, except for int iArray [10] = {1, 2} during initialization}
In this way, you cannot assign values to array elements in batches in other places.
3. At the same time, the strcpy replication function is for character type variables with '\ 0'. Therefore, this function assigns
The value is also out of range.
*/
It is estimated that the following method will be more robust:
Void test2 ()
{
Char string [10],
Str1 [10];
For (int I = 0; I <10; I ++)
Str1 [I] = 'a ';
Str [9] = '\ 0 ';
Strcpy (string, str1 );
}
Question 3:
Void test3 (char * str1)
{
Char string [10];
If (strlen (str1) <= 10)
{
Strcpy (string, str1 );
}
}
// Question 3 also has the possibility of crossing the border. If strlen (str1) = 10, str1 actually occupies 11 spaces.
// The length returned by the strlen function is not counted as '\ 0. Pay attention to this.
The following method may be more robust:
Void test3 (char * str1)
{
Char string [10];
If (strlen (str1) <10 & NULL! = Str1)
Strcpy (string, str1 );
}
Question 4:
Void GetMemory (char * p)
{
P = (char *) malloc (100 );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (str );
Strcpy (str, "hello world ");
Printf (str );
}
/*
First, the problem of Memory leakage and pointer pointing to a blank address exists.
There are several problems with this question:
1. In the GetMemory function, the return value of the malloc function is not tested.
If (NULL = p)
2. the pointer p is not released in the function.
Free (p );
3. There is a problem here. in C language, it is passed by value by default, not by address.
In the program, the point of str cannot be changed.
GetMemory (str); the point of str cannot be changed.
Function prototype:
Void GetMemory (char * p); defines a pointer type parameter.
4. The value of the pass-in parameter cannot be changed within the function.
*/
/*****************
The essence of the malloc function is that it connects available memory blocks into a long list of so-called idle linked lists. When calling the malloc function,
It searches for a memory block that is large enough to meet user requests along the connection table. Then, split the memory block into two parts (the size of one part and user request
The size of the other part is equal to the remaining bytes ). Next, pass the memory allocated to the user, and
If any) return to the connection table. When the free function is called, it connects the memory block released by the user to the idle chain. At the end, the idle chain will be cut into many
If the user requests a large memory segment, there may be no segment on the idle chain that meets the user's requirements. So,
Malloc function request latency, and began to rummaging through the idle chain to check the memory segments, sort them, and merge adjacent small idle blocks into large ones.
Memory block. If the required memory block cannot be obtained, the malloc function returns a NULL pointer. Therefore, when calling malloc to dynamically apply for a memory block, you must
Determine the return value.
**************/
The following format may be more robust:
Void GetMemory (char ** p)
{
Char * temp;
If (NULL! = (Temp = (char *) malloc (1000 )))
* P = temp;
Free (temp );
}
Question 5:
Char * GetMemory (void)
{
Char p [] = "hello world ";
Return p;
}
Void Test (void)
{
Char * str = NULL;
Str = GetMemory ();
Printf (str );
}
/* In fact, what should I do? This question is easier to understand than the previous one, but through this question and the above
You need to know the fact:
That is, the local variables declared in the function (except for static type and register type ).
It is an auto variable, and its memory space is applied in the stack opened by the system for applications.
The space applied for by the malloc function is applied from the heap opened by the system for the application. The application in the heap will not be automatically released,
The application in the stack will be automatically released as the function declaration cycle ends.
This question is wrong because it does not understand the lifecycle of local variables.
*/
The following format may be more robust:
Char * getmemory (void)
{
Char * p = NULL;
If (NULL! = (P = (char *) malloc (strlen ("hello word") + 1 ))
Return p;
}
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 );
}
/*
This topic has been implemented and discussed in the fourth topic.
It refers:
The value of & str cannot be changed.
*/
Question 7:
Void Test (void)
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Free (str );
... // Other omitted statements
}
/*
This question is simpler than above. The problem is that the malloc function is not returned.
Detection,
If NULL = (char *) malloc (NUM), The strcpy function will not be executed successfully,
*/
As I am a computer cainiao, I don't even have basic compatibility with DOS commands. I am also a cainiao in C language.
The content above is inappropriate. You are welcome to bend over to find the bricks ......................
Xi ..............................