catalogue
2, Detailed explanation of dynamic memory function and its use
2.1 overview of malloc and free functions
2.2 overview of calloc function
2.3 overview of realloc function
2.3.1 example use of realloc function
2.4 common errors in dynamic memory space
3, Common written test questions
3.1 written test questions for pass on questions
3.2 the problem of returning the address of stack space
1, Foreword
Do you know where the variables created during our previous programming are stored in memory?
In fact, different types of variables are stored in different locations in memory.
Let's take a look:
So why should there be dynamic memory development?
Sometimes the size of the space we need can only be known when the program is running. The way in which the array opens up space during compilation cannot be met. At this time, we need our dynamic memory allocation.
Dynamic memory development mainly requires us to understand and master four functions
1.malloc
2.calloc
3.realloc
4.free
2, Detailed explanation of dynamic memory function and its use
2.1 overview of malloc and free functions
1. malloc function: (document address: cplusplus.com).
2. free function
2.1.1 malloc example usage:
#include<stdlib.h> #include<string.h> #include<errno.h> #include<stdio.h> int main() { int* p = (int *)malloc(10 * sizeof(int)); //If the development fails //Be sure to check if(p == NULL) { //Print error reason printf("%s\n",strerror(errno)); } else { //Normal use space int i = 0; for(i = 0 ; i < 10 ; i++) { *(p+i) = i; } for(i = 0 ; i < 10 ; i++) { printf("%d\n",*(p+i)); } } //When the dynamically requested space is no longer used, the space should be returned to the operating system free(p);//Active recycling p = NULL; return 0; }
Profiling code:
If I open up a large memory space, what error message will be returned?
In fact, the operating system will release the space opened up by dynamic memory at the end of our program. Is it necessary for the free function to exist?
of course!!
How about the malloc and free functions?
2.2 overview of calloc function
2.2.1 calloc example usage
int main() { int* p = calloc(10,sizeof(int)); if(p == NULL) { //Print error reason printf("%s\n",strerror(errno)); } else { int i = 0; for(i = 0 ; i < 10 ; i++) { printf("%d ",*(p+i)); } } free(p); p = NULL; }
Code analysis:
You can choose whether to use malloc function or calloc function according to your actual needs.
2.3 overview of realloc function
2.3.1 example use of realloc function
int main() { int* p = (int *)malloc(5 * sizeof(int)); if (p == NULL) { printf("%s\n", strerror(errno)); } else { int i = 0; for (i = 0; i < 5; i++) { *(p + i) = i; } for (i = 0; i < 5; i++) { printf("%d ", *(p + i)); } } printf("\n"); //Here I want to open up 20 spaces for me to use int* p2 = (int *)realloc(p, 5* sizeof(int )); if(p2 != NULL) //Append succeeded { p = p2; } int i = 0; for (i = 5; i < 10; i++) { *(p + i) = i; } for (i = 5; i < 10; i++) { printf("%d ", *(p + i)); } free(p); p = NULL; return 0; }
Code analysis:
Think about it:
2.4 common errors in dynamic memory space
1. Dereference null pointer
There is no need to introduce this error. Of course, NULL pointers cannot be dereferenced
2. Cross border access to dynamic memory
Feel this error in the upper code
int *p = (int *)malloc*(5 *sizoef(int)); if(p == NULL) { return 0; } else { for(int i = 0 ; i < 10 ; i++) { *(p+i) = i; } free(p); p=NULL; }
The mistake here is obvious. Only five spaces have been opened up. You need to visit ten. It's a little too much ~.
3. Perform free operation on non dynamically opened memory
int main() { int p = 10; free(p); }
p is not dynamically developed here. If you release it with free, an error will be reported.
4. Release the same dynamic memory for multiple times.
3, Common written test questions
3.1 written test questions for pass on questions
void GetMemory(char *p) { p = (char *)malloc(100 *sizeof(int )); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str,"Hello World!\n"); printf(str); } int main() { Test(); return 0; }
Will the above code output correctly?
Obviously, there is no output of "Hello World!",
There is a problem with this code.
So what's the problem?
Therefore, this is a good proof of why there is no output after the program runs.
So how to correct it?
Remember? When the value transmission is not possible, we can send the address.
Run successfully
void GetMemory(char **p) { *p = (char *)malloc(100 * sizeof(int)); } void Test(void) { char *str = NULL; GetMemory(&str); strcpy(str, "Hello World!\n"); printf(str); free(str); str = NULL; } int main() { Test(); return 0; }
Remember to release the dynamically opened space and set the pointer to null.
3.2 the problem of returning the address of stack space
char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } int main() { Test(); return 0; }
Will this code print "Hello World!" smoothly?
Let's have a look.
I'll go. What's this? Why print out such a ghost?
Don't worry, let's have a look
So, what comes back here, none of us knows what it will be.
3.3 causing memory leakage
void Test(void) { char *str = (char *)malloc(100); strcpy(str, "hello"); free(str); if (str != NULL) { strcpy(str, "world"); printf(str); } } int main() { Test(); return 0; }
Is there a problem with this code?
It's printed correctly.
But there's no problem printing it out?
Therefore, after we write the free function, we have to set the pointer to NULL. To prevent such non-standard behavior from causing irreparable consequences.
void Test(void) { char *str = (char *)malloc(100); strcpy(str, "hello"); free(str); str = NULL; if (str != NULL) { strcpy(str, "world"); printf(str); } } int main() { Test(); return 0; }
over~~~