malloc

void *malloc( size_t size );

Tips: there size Represents the size of bytes

malloc Use of :
//malloc Use of #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include
<string.h> #include <errno.h> #include <stdlib.h> int main() { int* str = 0;
int* p = 0; str = (int*)malloc(10*sizeof(int));// Open up ten integer spaces if (NULL == str) {
printf("%s\n", strerror(errno));// If the development fails // Use error reporting function strerror(errno)
Header file to reference <string.h> } else { p = str; } free(p); p = NULL; return 0; }
free

Free requested memory space , example :free(p)

When released , although p The value in is still there , unchanged , but p It's a wild pointer . So it will be recommended after release p Set to null pointer .(p=NULL)

calloc

calloc: Open and initialize as 0 Array of .

void* calloc(size_t num,size_t size)

* num—— Number of elements
* size—— Element size
Return address if successful , Null pointer returned on failure NULL

calloc Use of :
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h>
#include <errno.h> #include <stdlib.h> int main() { int* str = 0; int* p = 0;
str = (int*)calloc(10,sizeof(int)); if (NULL == str) { printf("%s\n",
strerror(errno)); } else { p = str; } free(p); p = NULL; return 0; }
realloc

Exploitable space , You can also adjust the space .

void *realloc( void *memblock, size_t size );

* memblock—— Pointer type to open up space
* size—— Size of bytes to be opened
p=(int)realloc(p,80)*—— Writing like this is also risky .

risk :
In order to avoid the possibility of overwriting the existing memory space behind the capacity increase , So it will open up space in another place of sufficient size , Then transfer the original data to the new space . And free up the original memory space .

if realloc Failed to adjust space , Then return NULL. The original data is gone .

realloc Use improvement of :
int* ptr=(int*)realloc(p,80); if(NULL!=ptr) {
p=ptr;// In this way, it can ensure that the non null pointer is determined before it is officially passed to the p, Equivalent to no risk of losing the original data }
realloc Another use of :

int* p=(int*)realloc(NULL,40); This is equivalent to malloc

Common dynamic memory errors

Dereference of null pointer

take malloc Function opens up a big space ,INT_MAX, There will be a null pointer , Judge , If the pointer is null, the program ends immediately . Don't go wrong (ps: there INT_MAX Use the header file to reference limits.h)

Therefore, it is necessary to judge whether it is a null pointer , If so, interrupt , example :
// Wrong writing #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include
<limits.h> #include <string.h> #include <errno.h> #include <stdlib.h> int
main() { int i = 0; int* p = (int*)malloc(INT_MAX); for (i = 0; i < 5; i++) {
*(p + i) = i; } return 0; } // Correct writing : #define _CRT_SECURE_NO_WARNINGS 1 #include
<stdio.h> #include <limits.h> #include <string.h> #include <errno.h> #include
<stdlib.h> int main() { int i = 0; int* p = (int*)malloc(INT_MAX); if (p ==
NULL) { printf("%s\n", strerror(errno));// Here is to report the error return 0;// Null pointer found , Early end }
for (i = 0; i < 5; i++) { *(p + i) = i; } return 0; }
Cross border access to dynamic open space

Dynamic memory space cannot be used without application , It will report an error .

Tips: A space that has not been opened up cannot be used

Use of non dynamic memory free release
int main() { int p=0; int* a=&p; free(a);// This is wrong return 0; }
use free Freeing a piece of dynamic memory

When opening up dynamic space , Be sure to save the starting position for the variable , Otherwise, the memory cannot be released at that time .
// use free Freeing a piece of dynamic memory // Correct writing : #define _CRT_SECURE_NO_WARNINGS 1 #include
<stdio.h> #include <stdlib.h> int main() { int i = 0; int* p = (int*)malloc(10
* sizeof(int)); // Correct writing : for (i = 0; i < 5; i++) { *(p + i) = i; } free(p);
p=NULL; return 0; } // Wrong writing : #define _CRT_SECURE_NO_WARNINGS 1 #include
<stdio.h> #include <stdlib.h> int main() { int i = 0; int* p = (int*)malloc(10
* sizeof(int)); // Wrong writing : for (i = 0; i < 5; i++) { *p = i;
p++;// This will change p Original location of , This makes it impossible to point to the location where dynamic memory space was initially opened up , Final error report } free(p); p = NULL; return 0; }
Multiple releases of the same dynamic memory

A space cannot be released after it is released , But after the release p Set to null. No error will be reported when the pointer is released again .

Q:free Is there a problem with null pointers ?

A: can't , Because after a space is released, it can't be released again , So every time free Remember to set to null pointer after completion .

Dynamic memory forgetting to release ( Memory leak )

Even if you open up memory space in a function, remember to free it . Because there is a function, you can't release it if you want to release it outside .

But if you return the address of the first element ,free It's OK to go , No matter what , Be sure to release .

It is best to free the memory space opened up anywhere .

Find out the following problems :

T1:
void GetMemory(char* p) { p=(char*)malloc(100); } void Test(void) { char*
str=NULL; GetMemory(str); strcpy(str,"hello world"); printf(str); } int main()
{ Test(); return 0; }
Problems arising :

 
here str Is a null pointer , and p Just a new parameter , Cannot return after running the function p It doesn't exist , However, the memory space has not been released , No one can know the address of this space at this time . Nor can it be str Inside NULL change , So in strcpy There will be an error when , because str At this time NULL Pointer , It will cause illegal access to memory , The program will crash .

Moreover, only dynamic memory is developed in the process of use , Dynamic memory is not released , May cause dynamic memory leaks .

Improvement method :
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h>
#include <string.h> 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; }
Stack frame and creation of function :p Although destroyed , Because I'll put it first p Put the value in the register , The register will not be destroyed , Then pass it in from the register position str.

 

T2:

Problems arising :

Return stack space address problem :

Although it can p Send your address back , However, after the function runs, the data created in the function will be destroyed , In other words, although you can find the place pointed to by the original memory through the pointer , But the data was destroyed .

be careful !!!

This is OK , Because the return is the variable of stack space, not the address of stack space .

summary :

When creating a function, if it returns an address instead of a return value , When used, it may still be the value in the function , But there's a good chance not , Maybe not because of the function stack frame , If you write a paragraph before the reference address, for example :"printf("23333\n");", It may cause the data on the original address to be overwritten , Therefore, the real value cannot be output through address transmission , Because it will be covered .

T3:

Problems arising :

except free There's not much wrong with it . Can print out here hello.

T4:

  Problems arising :

there free In fact, it returns the dynamic memory space to the system , however str Is not set as a null pointer , There is still the address pointing to the opened memory space , Then you can still pass str Find the original memory space , Just this time because of the release (free)str Yes , Therefore, there is no permission to access the space at this time , You can't world copy to str The space pointed to .

Correct modification :

therefore , Every time free Remember to set it to null pointer later .

Flexible array

stay c99 in , The last element in the structure is an array of unknown sizes allowed , this is known as 【 Flexible array 】 member .

Definition of flexible array
// Writing method I : struct s1 { int n; int arr[0];// The size is unspecified } // Writing method 2 : struct s2 { int n; int
arr[];// The size is unspecified } // There is always a way to write, and the compiler does not report an error
Tips: When calculating the size of a flexible array , Flexible arrays are not counted in size .( You can write one to try )

Characteristics of flexible array :

* At least one other member is required before a flexible array
* sizeof The size of this structure returned does not include the memory of the flexible array
* Structure containing flexible array members malloc() Function to dynamically allocate memory , And the allocated memory should be larger than the size of the structure , To accommodate the expected size of the flexible array .
Development of flexible array ( Write it yourself )

Structures containing flexible arrays cannot be created directly , It's about having malloc To open up space .
// Method 1 of writing flexible array : #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include
<stdlib.h> #include <string.h> struct p { int i; int arr[]; }; int main() {
struct p* cmp = (struct p*)malloc(sizeof(struct p) +
80);// Here is a total of 84 Byte space , Distribute arr array 80 Byte space free(p); p = NULL; return 0; }
// Method 2 of writing flexible array :( Open up the whole first , Then open up the array ) #define _CRT_SECURE_NO_WARNINGS 1 #include
<stdio.h> #include <stdlib.h> #include <string.h> struct p { int i; int*
arr;// This can be used in method 2 }; int main() { struct p* cmp = (struct
p*)malloc(sizeof(struct p)); cmp->i = 10; cmp->arr =
(int*)malloc(80);// Start with array , Open up again 80 Byte space free(p); p = NULL; return 0; }
The second scheme ( inferiority ):

* Many times of development and release , Error prone
* Open up memory frequently and many times , There will be memory fragments , It may lead to inefficient use of memory
Advantages of the first scheme :

* Convenient release
* Reduce memory fragmentation

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq