In dynamic memory allocation, the classic error cases are as follows:
Case 1:
Original code:
void getmemory(char* p)//Should be changed to char** p { p = (char*)malloc(100);//The opened space address is not returned to str in the test function //Should read * p } void test(void) { char* str = NULL; getmemory(str); strcpy(str, "hello world");//str does not point to a valid space. The program crashes printf(str); } int main() { test(); return 0; }
This code will crash when running for the following reasons:
The program has the problem of memory leakage. str is passed to p in the form of value transfer. p is the formal parameter of getmemory function and is only valid inside getmemory function. When getmemory function returns, the dynamic development memory has not been released and cannot be found, so there is memory leakage.
Correction 1:
void getmemory(char **p) { *p = (char *)malloc(100); } void test(void) { char *str = NULL; getmemory(&str); strcpy(str, "hello world"); printf(str); free(str); str = NULL; } int main() { test(); return 0; }
Pass the value pointed to by str to the getmemory function by addressing, that is * * p, and accept the space opened by the malloc function in the form of pointer * p in the getmemory function;
In addition, memory release is added.
Correction 2:
char* getmemory(char* p) { p = (char*)malloc(100); return p; } void test(void) { char* str = NULL; str=getmemory(str); strcpy(str, "hello world"); printf(str); free(str); str = NULL; } int main() { test(); return 0; }
In the getmemory function, add the < return P > code to return the opened space address. The opened space can be used in the main function and released after use.
Case 2:
Original code:
char* getmemory(void) { char p[] = "hello world";//Local variable, valid only within this function return p; } void test(void) { char* str = NULL; str = getmemory(); printf(str); } int main() { test(); return 0; }
As shown in the code comments, the variable p defined in the getmemory function is a local variable and cannot be used in other functions.
Similarly:
int* test() { int a = 10;//Stack err return &a; } int main() { int*p = test(); return 0; }
In the above code, the integer variable a defined in the test function is a local variable, which is stored in the stack and cannot be used in the main function.
take
int a = 10;
Amend to read:
static int a = 10;
In this way, the variable a is stored in the static area and can be used by the main function.
The corrected complete code is as follows:
int* test() { static int a = 10; return &a; } int main() { int*p = test(); return 0; }
Similarly:
int* test() { int* ptr = malloc(100);//The open space is in the heap area. No free still exists. It can be passed back to the main function return ptr; } int main() { int* p = test(); return 0; }
Although the pointer variable ptr in the above code segment is defined inside the test function, static defines it as a static pointer variable, which is stored in the static area. free still exists in the static area and can be used in the main () function.
Case 3:
Original code:
void getmemory(char** p, int num) { *p = (char*)malloc(num); } void test(void) { char* str = NULL; getmemory(&str, 100); strcpy(str, "hello"); printf(str); } int main() { test(); return 0; }
The above code does not release the dynamically opened memory, resulting in memory leakage.
After using the space opened by dynamic memory, add the following code to free the memory space:
free(str);// str = NULL;//These two lines of code free up space
The corrected code is as follows:
void getmemory(char** p, int num) { *p = (char*)malloc(num); } void test(void) { char* str = NULL; getmemory(&str, 100); strcpy(str, "hello"); printf(str); free(str);// str = NULL;//These two lines of code free up space } int main() { test(); return 0; }
Case 4:
Original code:
void test() { char* str = (char*)malloc(100); strcpy(str, "hello"); free(str); if (str != NULL) { strcpy(str, "world"); printf(str); } } int main() { test(); return 0; }
The above free function releases the memory space pointed to by str, but does not set str to NULL, so the following if judgment statement is meaningless.
Precede the if statement with
str = NULL;
The code after correction is:
void test() { char* str = (char*)malloc(100); strcpy(str, "hello"); free(str);//The memory space pointed to by STR is released here, and str will not be set to NULL str = NULL; } int main() { test(); return 0; }
Case 4 is very confusing. I can't understand it. I can understand it.