当前位置:网站首页>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 )
边栏推荐
- Is it safe to open a stock account through the QR code of the securities manager? Or is it safe to open an account in a securities company?
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- Some things configured from ros1 to ros2
- Develop scalable contracts based on hardhat and openzeppelin (I)
- GGPlot Examples Best Reference
- [cloud native] 2.5 kubernetes core practice (Part 2)
- 揭露数据不一致的利器 —— 实时核对系统
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- 数据分析 - matplotlib示例代码
猜你喜欢

A sharp tool for exposing data inconsistencies -- a real-time verification system

GGPlot Examples Best Reference

可昇級合約的原理-DelegateCall

Attribute acquisition method and operation notes of C # multidimensional array

多文件程序X32dbg动态调试

在连接mysql数据库的时候一直报错

RPA advanced (II) uipath application practice

Compilation errors and printout garbled problems caused by Chinese content in vs2019 code

Principe du contrat évolutif - delegatecall

揭露数据不一致的利器 —— 实时核对系统
随机推荐
Astparser parsing class files with enum enumeration methods
PowerBI中导出数据方法汇总
mysql 基本语句
MySQL linked list data storage query sorting problem
Redis超出最大内存错误OOM command not allowed when used memory > 'maxmemory'
Is the stock account given by qiniu business school safe? Can I open an account?
YYGH-BUG-05
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
R HISTOGRAM EXAMPLE QUICK REFERENCE
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
CMake交叉编译
easyExcel和lombok注解以及swagger常用注解
由粒子加速器产生的反中子形成的白洞
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Skills of PLC recorder in quickly monitoring multiple PLC bits
进入前六!博云在中国云管理软件市场销量排行持续上升
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
【多线程】主线程等待子线程执行完毕在执行并获取执行结果的方式记录(有注解代码无坑)
Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'
电脑无缘无故黑屏,无法调节亮度。