当前位置:网站首页>Dynamic memory (advanced 4)
Dynamic memory (advanced 4)
2022-07-02 11:49:00 【Less debug every day】
realloc
malloc And calloc It's just calloc While opening up space , The number in space will also be assigned 0
Let's focus on realloc
void* realloc (void* ptr, size_t size);
ptr Is the memory address to be adjusted
size New size after adjustment
The return value is the starting memory position after adjustment .
This function adjusts the size of the original memory space , The original data in memory will also be moved to new Space .
realloc There are two ways to adjust memory space :
situation 1: There is enough space behind the original space
situation 2: There is not enough space after the original space
free The understanding of the
1,free(p) Just make the system recycle the space , Not destroying space , The space still exists .
2,free(NULL) There is no problem , because NULL There is no point to effective space
3,free It can only be used in dynamic space , The space on the stack area cannot be released
4, A space cannot be released many times , Suppose you release it once and the system uses it again , When you release again , It will destroy other effective spaces
5,free Be sure to set the pointer to NULL
Flexible array
Mainly when you need to use dynamically growing arrays in the structure
struct A
{
int a;
int arr[0];
// perhaps int arr[];
};
Flexible arrays can be equivalent to dynamically growing arrays, but they are somewhat different when used
The characteristics of flexible arrays
A flexible array member in a structure must be preceded by at least one other member .
sizeof The size of the structure returned does not include the memory of the flexible array .
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 , To fit the expected size of the flexible array
In fact, from a functional point of view , It's not impossible for us to develop arrays dynamically alone
You can do that
A* p = (A*)malloc(sizeof(struct A) );
p->a = (int*)malloc(sizeof(int) * 100);
This is also opening up for arrays 100*4 Bytes of space , But if you do this, it will be more troublesome in the subsequent memory release , The latter frees up more space than the former , When users are using our functions , I don't know that I made a secondary memory allocation , Users may only free memory once , Then it will cause memory leak , So we allocate the memory of the structure and its member memory at one time , And return a pointer , Users only need to do it once free That's all right. .
meanwhile Continuous memory helps to improve access speed , It also helps reduce memory fragmentation .
( Because the allocated space is discontinuous , The array occupies continuous space )
边栏推荐
- PX4 Position_Control RC_Remoter引入
- JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
- Attribute acquisition method and operation notes of C # multidimensional array
- 念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研
- Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'
- How to Add P-Values onto Horizontal GGPLOTS
- What week is a date obtained by QT
- Tdsql | difficult employment? Tencent cloud database micro authentication to help you
- Summary of flutter problems
- 行业的分析
猜你喜欢
MySQL linked list data storage query sorting problem
K-Means Clustering Visualization in R: Step By Step Guide
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
Summary of data export methods in powerbi
解决uniapp列表快速滑动页面数据空白问题
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
在连接mysql数据库的时候一直报错
动态内存(进阶四)
抖音海外版TikTok:正与拜登政府敲定最终数据安全协议
ESP32 Arduino 引入LVGL 碰到的一些问题
随机推荐
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
The selected cells in Excel form have the selection effect of cross shading
SSRF
Is the Ren domain name valuable? Is it worth investing? What is the application scope of Ren domain name?
Programmer growth Chapter 6: how to choose a company?
STM32 single chip microcomputer programming learning
Develop scalable contracts based on hardhat and openzeppelin (II)
PYQT5+openCV项目实战:微循环仪图片、视频记录和人工对比软件(附源码)
Is it safe to open a stock account online? I'm a novice, please guide me
对毕业季即将踏入职场的年轻人的一点建议
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
原生方法合并word
What is the relationship between digital transformation of manufacturing industry and lean production
MySql存储过程游标遍历结果集
Wechat applet uses Baidu API to achieve plant recognition
Take you ten days to easily finish the finale of go micro services (distributed transactions)
C file and folder operation
YYGH-BUG-05
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
[cloud native] 2.5 kubernetes core practice (Part 2)