当前位置:网站首页>Dynamic memory management (1)
Dynamic memory management (1)
2022-06-24 22:36:00 【Code loving students】
1. Why is there dynamic memory allocation
At present, the way we know to open up space is :
int val = 20; // Open up four bytes in the stack space
char arr[10] = {0}; // Open up on the stack space 10 Bytes of contiguous space
But there are two characteristics of the way to open up space :
1. The size of the space opening is fixed .
2. Arrays are declared , You must specify the length of the array , The memory it needs is allocated at compile time .
But on the way to writing code , The situation is changeable , The fixed space size may not be enough for operation , Then we can't change the size , We introduced a new memory allocation method —— Dynamic memory .
2. The introduction of dynamic memory function
2.1 malloc and free
In the above question ,C Language provides a dynamic development function ——malloc function
1. This function requests a piece of memory Continuously available Space , And return the pointer to this space .
2. If the development is successful , Then return a pointer to open a good space .
3. If the development fails , Returns a NULL The pointer , therefore malloc The return value of must be checked .
4. The type of return value is void* , therefore malloc Function doesn't know the type of open space , Specifically, when using, the user can
It's up to me to decide .
5. If parameters size by 0,malloc The standard is undefined , Depends on the compiler .
C Language provides another function free, It is specially used for dynamic memory release and recovery , The function prototype is as follows :
void free (void* ptr);
free Function is used to release dynamically opened memory .
1. If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined .
2. If parameters ptr yes NULL The pointer , Then the function does nothing .
malloc and free It's all stated that stdlib.h Header file .
so to speak malloc and free It's used together .
2.2 calloc function
C The language also provides a function called calloc , calloc Function is also used for dynamic memory allocation . The prototype is as follows :
void* calloc (size_t num, size_t size);
1. The function is for num Size is size The elements of open up a space , And initialize each byte of the space to 0.
2. And functions malloc The only difference is calloc Initializes each byte of the requested space to full before returning the address 0.
So how do we request initialization for the contents of the requested memory space , So it's easy to use calloc function To get the job done .
2.3 realloc function
1. realloc Functions make dynamic memory management more flexible .
2. Sometimes we find that the application space is too small in the past , Sometimes we think the application space is too large , That's for reasonable time memory , We will make flexible adjustments to the size of memory . that realloc Function can achieve large memory for dynamic development
Small adjustments .
The function prototype is as follows :
void* realloc (void* ptr, size_t size);
1. ptr Is the memory address to be adjusted
2. size New size after adjustment
3. The return value is the starting memory position after adjustment .
4. This function adjusts the size of the original memory space , It will also move the data in the original memory to a new space .
边栏推荐
- How to extract dates from web pages?
- 04A interrupt configuration
- 如何比较两个或多个分布:从可视化到统计检验的方法总结
- Data communication foundation - Ethernet port mirroring and link aggregation
- socket done
- Huada 4a0gpio settings
- New features of go1.18: efficient replication, new clone API for strings and bytes standard library
- HTTP的缓存控制
- 虚拟人的产业发展现状
- How to solve the problem that the computer suddenly can't connect to WiFi
猜你喜欢

一个女孩子居然做了十年硬件。。。

虚拟人的产业发展现状

Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?

Selection and comparison of message oriented middleware MQ

Cache control of HTTP

MySQL + JSON = King fried!!

Redis-跳表

Virtual private network foundation

DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别

NIO、BIO、AIO
随机推荐
04A interrupt configuration
Data communication foundation - Ethernet port mirroring and link aggregation
Yyds dry goods inventory junit5 learning II: assumptions class
Power system | IEEE paper submission process
Why can some programmers get good offers with average ability?
CDN principle
Valueerror: cannot take a larger sample than population when 'replace=false‘
Data center basic network platform
AQS source code analysis
Redis-跳表
In the era of industrial Internet, there is no Internet in the traditional sense
Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
Development of live broadcast software app, and automatic left-right sliding rotation chart advertising
try-with-resources 中的一个坑,注意避让
Process communication mode
Zero code can apply data visualization to enterprise management
Redis hop table
OA system -- save the verification code to session
ansible基本配置
How does flutter use the online transcoding tool to convert JSON to model