当前位置:网站首页>Application of C language dynamic memory function
Application of C language dynamic memory function
2022-06-22 01:30:00 【51CTO】
Application of dynamic memory allocation function
The dynamic memory allocation functions are :malloc,calloc,realloc and free, Here is how they are used
1.malloc function
malloc The function is used to apply for a specified byte size of space from the heap , The function parameter is the byte size of the requested space , Return value is one void* The pointer to , If the application fails, it will return NULL, The usage is as follows :
2.calloc function
calloc Functions and malloc Function similarity , It is also used to apply to the heap to open up memory space and return the address ,calloc The first parameter of the function is the number of elements stored in the application space , The second parameter is the byte size of the element type , Function return value type is void*, Besides calloc The function initializes each element of the space as 0, The usage is as follows :
realloc function
realloc Function is a very important function in dynamic memory allocation , It can adjust malloc and calloc The size of the requested space , Make it more flexible ,realloc The first argument to the function is a pointer to a dynamic memory space , The second parameter is the modified space size , The return value is the modified space address , Use as follows :
realloc Function considerations :
1. If p There is enough memory after the space pointed to can be added directly , Return address after .
2. If p There is not enough memory space to append after the space pointed to , be realloc Will find a new memory area to open up a space to meet the needs , And copy back the data in the original memory , Free up old memory space , Finally, return the newly opened memory space address .
3. To receive... With a new variable realloc The return value of the function
4.free
free The function should be related to malloc/calloc Functions are used in pairs , It is by releasing space , However, the released pointer is not set to null. The pointer still has the ability to find the originally opened dynamic memory space, which will only cause illegal access , So use it again free When you free up space, set the pointer to NULL. Besides free The released memory space must be dynamically opened up ,free A null pointer means nothing .
边栏推荐
- 記錄webscraper的使用過程
- 52 classes 110 common components and frameworks
- Handwriting database connection pool
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql. sock' (2)
- Use of listctl virtual mode under wince
- 【CYW20189】七、HCI命令格式详解
- Documenter l'utilisation de webcraper
- pm2 的学习
- 4275. Dijkstra sequence
- FLowable运行时事务相关的表和表结构
猜你喜欢

Pytorch learning 11:where and gather

Cancellation meeting and automatic refund processing of message queue

IDEA 提示 Duplicated code fragment (15 lines long)

SSO and oauth2 solutions

matplotlib 制作不等间距直方图
![[environment stepping on the pit] open the picture with OpenCV and report an error](/img/6d/4679cfebf2dfd43566c976d435ea84.png)
[environment stepping on the pit] open the picture with OpenCV and report an error

Handwriting database connection pool

SAP MM 进口采购业务中供应商多送或者少送场景的处理

How to use the low code platform of the Internet of things for report management?

Today's content
随机推荐
Gaode map -- obtain longitude and latitude according to geographical location
Install tensorflow and transformer on Orange Pie orangepi4b
Unlovable STL
How to remove duplication in left join from a simple example
Error 4 opening dom ASM/Self in 0x8283c00
Differences among active window, focused window and foreground window
Judge whether the system CPU is idle
Compilation principle - recursive descent subroutine method
03 FastJson 解决循环引用
After the counter is completed, you want to count the results whose string length is greater than 2
1876. 长度为三且各字符不同的子字符串
[dailyfresh] problems encountered in sending activation email
Interview question catalog collection
yolov3 3D 语义点云paper阅读
Navicat连接不到MySQL
Counter完之后,想统计字符串长度大于2的结果
[Others] analysis of got and PLT in ELF
English grammar_ Adverb_ fairly / rather
IDEA提示 ‘Optional.get()‘ without ‘isPresent()‘ check错误。
Evc4 program cannot run on the emulator