当前位置:网站首页>Dynamic memory related notes
Dynamic memory related notes
2022-07-20 21:51:00 【Generally, I passed by banyuanjun】
C In language , Every time you create a variable , The system will automatically open up a space corresponding to the size of type bytes for a variable , But sometimes this is not enough , therefore C There are several functions in the language , For users to apply for space , This is called dynamic memory management , Here I will introduce the functions of dynamic memory and common errors .
( The following functions contain header files <stdlib.h> in )
void * malloc(size_t size);
The first function is malloc function , This function will apply for a size Size of byte space , And return the first address of this space , But the return is a void* Pointer to type , So before using, you need to force the conversion type to divide the use .
void * calloc(size_t num,size_t size);
The second function is calloc, And malloc Different , This function requires two parameters , First parameter num Indicates the number of spaces to be opened , and size Represents the size of each space , also calloc The function initializes all the space it opens to 0, Of course , It also returns a void* Function of type , A cast is required .
void * realloc (void *ptr,size_t size);
This realloc The job of function is actually to expand space , The first parameter ptr Is the first address of the space to be adjusted , The second parameter size Is how many bytes to adjust ,
Such as
int * p = (int *) malloc(40);
int *pt = (int *)realloc(p,80);
if(pt!=NULL)
p = pt;
This will be p Space from 40 Expanded to 80 Bytes ;
however ,realloc The function developed is actually divided into two different cases ;
1> The subsequent space of the pointer can be expanded to the required space , Just expand directly
2> The subsequent space of the pointer cannot be expanded to the required space , You will find enough space in the heap , After opening up , Put the elements of the old space in the new space , Release the memory in the old space .
void* free(void *ptr);
The previous three functions are used to open up space , The space opened up by the three functions is in the heap , Unless the program closes , Otherwise, the memory will not be released automatically , So there needs to be free Function is used to release the opened space , Otherwise, it will cause problems such as memory leakage .
free The function is very simple , The required parameter is the first address to free space , But one thing to note is ,free Although the function can free up space , But the address pointed to by the pointer still exists , And the space pointed by the pointer is open , This causes the problem of wild pointer , So we use free After the function frees up the corresponding space , The corresponding pointer should be set to NULL.
Such as :
int * p = (int *)malloc(40);
if(p!=NULL)
{
free(p);
p = NULL;
}
Common errors in dynamic memory
In the previous code snippet , Every time I open space, I will first judge whether this pointer is empty , It will only be used if it is not empty , This is actually because when malloc calloc realloc When three functions fail to open space , Will return a NULL, and NULL Pointers are useless , So we must judge and then , At this time, one of the common errors in dynamic memory development —— Yes NULL Pointer dereference .
Common errors in dynamic memory are 6 Kind of ,
1> Yes NULL Pointer dereference
2> Cross border visits to open up space
3> Use of non dynamic space free function
4> Use free Function to release part of the dynamically opened memory
5> Release the same space multiple times
6> Dynamic memory is not released ( Memory leak )
Flexible array
stay C99 In the standard , Struct allows the last member to be an array of unknown size , This is the flexible array member ,
Such as :
struct S
{
int i;
int a[];
};
In structure S in ,a Array is a typical flexible array .
Flexible arrays have the following characteristics :
1> A flexible array member in a structure must be preceded by at least one other member .
2>sizeof The size of the structure returned does not include the memory of the flexible array .
3> Structures that contain flexible array members use malloc () Function to dynamically allocate memory , And the allocated memory should be larger than the size of the structure
Small , To fit the expected size of the flexible array
Flexible arrays should use malloc To allocate its memory , Use as follows :
struct S
{
int i;
int a[];
};
int main()
{
struct S* s;
s = (struct S*)malloc(sizeof(struct S)+40);
return 0;
}
such , This structural variable s It's a distribution 44 Byte space , front 4 Bytes are members i Space , The array after a Was assigned 40 Bytes of space , however , In fact, using pointer type variables in structures can do similar things , But flexible arrays have their own convenience , Advantages as follows :
1> Convenient memory release
2> Conducive to access speed
Of course , Flexible arrays can not only be used malloc Allocate space , You can also use realloc To change the size of the space , Grammar is the same as others .
边栏推荐
猜你喜欢
手撕快速排序
STM32 HAL库串口同时收发,接收卡死?
2022河南萌新联赛第(二)场:河南理工大学 F - 手办
走进创客教育课程实践的真实情境
机器人时代发展大趋势对民众的影响
The LAAS protocol of defi 2.0 is the key to revitalizing the development of defi track
30 open source software most popular with IT companies
马斯克称已将大脑上传到云端【系统或已开源】
JSON format interface test process
森马做LP的背后,“温州系”正跑步进入创投圈
随机推荐
LVGL 8.2 Span
FFmpeg 音视频截取
2022河南萌新联赛第(二)场:河南理工大学 A - 妙手
如何使用IDE工具HHDBCS,在Oracle数据库中创建一个包含1000条模拟数据的数据表,并将该
The application could not be installed: INSTALL_FAILED_USER_RESTRICTED
最受IT公司欢迎的 30 款开源软件
FFmpeg 视频解码
CLion编译和使用动态库
Eolink 和 JMeter 接口测试优势分析
PMP每日一练 | 考试不迷路-7.19
手撕快速排序
Win11 体验
Is it safe for Dongguan securities to buy shares and open an account?
在公司解决的问题
解析创客教育课程设置中的创新思维
DIY can decorate the mall system, you can also have!
July 18, 2022, village of p6722 "mcoi-01" in Luogu
索引下推的基本原理
2022年湖南工学院ACM集训第四次周测题解
Gson study notes