当前位置:网站首页>Dynamic memory management (1)
Dynamic memory management (1)
2022-06-11 21:54: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 .
边栏推荐
猜你喜欢

如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版

EndnoteX9简介及基本教程使用说明

即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍

ESP32C3 Arduino库使用方法

R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model

RPA+低代码为何是加速财务数字化转型之利器?

判断大小端存储两种办法

JVM | runtime data area; Program counter (PC register);

The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success

servlet获取表单数据
随机推荐
The shortcomings of the "big model" and the strengths of the "knowledge map"
揭秘爆款的小程序,为何一黑到底
R语言相关文章、文献整理合集(持续更新)
Redis basic data type (list)
238.除自身以外数组的乘积
On the night of the joint commissioning, I beat up my colleagues
Rexroth overflow valve zdb6vp2-42/315v
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
Leetcode-155-minimum stack
华为设备配置HoVPN
判断大小端存储两种办法
JVM class loader; Parental delegation mechanism
RPA+低代码助推品牌电商启新创变、重启增长
[niuke.com] ky41 put apples
Leetcode-98- validate binary search tree
C语言实现八种排序(1)
LabVIEW Arduino electronic weighing system (project Part-1)
Collection of articles and literatures related to R language (continuously updated)
Redis data type (string)