当前位置:网站首页>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 .
边栏推荐
- [niuke.com] dp31 [template] complete Backpack
- Customer information management software
- 实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
- Redis basic data type (set)
- Servlet get form data
- How to use the transaction code sat to find the name of the background storage database table corresponding to a sapgui screen field
- 不使用加减乘除做加法
- 如何利用RPA机器人开启货代行业数字化转型第一步?
- 联调这夜,我把同事打了...
- Educational Codeforces Round 114 (Rated for Div. 2) D
猜你喜欢

Popular science | what are the types of NFT (Part 1)

行而不辍,未来可期|云扩科技入选上海市专精特新企业

Classes and objects (3)

How to use the transaction code sat to find the name trial version of the background storage database table corresponding to a sapgui screen field

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

《物联网开发实战》18 场景联动:智能电灯如何感知光线?(上)(学习笔记)

RPA+低代码助推品牌电商启新创变、重启增长

类与对象(3)

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
随机推荐
Classes and objects (4)
Leetcode - 第2天
华为设备配置H-VPN
RPA+低代码为何是加速财务数字化转型之利器?
带有 ceph-csi 的静态 PVC
Leetcode-110-balanced binary tree
JVM | runtime data area; Program counter (PC register);
Take off efficiently! Can it be developed like this?
Parker plunger pump pv180r1k1t1nmmc
Go encoding package
判断大小端存储两种办法
联调这夜,我把同事打了...
Flink error: multiple tasks are started, and only one task is executed
每日一题 -- 验证回文串
RPA丨首席财务官如何找到数字化转型“超级入口”?
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
Educational Codeforces Round 114 (Rated for Div. 2) D
实验10 Bezier曲线生成-实验提高-控制点生成B样条曲线
Binary search - Learning