当前位置:网站首页>Dynamic memory management
Dynamic memory management
2022-07-23 10:56:00 【VisionaryHS】

Catalog
malloc function

- First understand malloc The data type of the function :1. The return value is void* The pointer , Parameter is unsigned integer size_t,size Indicates the size of the space you need to open , The unit is byte
- matters needing attention : If the development is successful , Then return a pointer to open a good space .If the development fails , Returns a NULL The pointer , therefore malloc The return value of must be checked .The type of return value is void* , therefore malloc Function doesn't know the type of open space , Specifically, when using, the user himselfTo decide .malloc The statement in stdlib.h Header fileIf parameters size by 0,malloc The standard is undefined , Depends on the compiler . Better not let size by 0;
- Use up malloc Remember to use free Function frees the space pointed to ;
free function
- free Function function :free Function is used to release memory opened dynamically .
- If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined .If parameters ptr yes NULL The pointer , Then the function does nothing .
- free The statement in stdlib.h Header file
- The specific usage is as follows :
#include <stdio.h> int main() { // Code 1 int num = 0; scanf("%d", &num); int arr[num] = {0}; // Code 2 int* ptr = NULL; ptr = (int*)malloc(num*sizeof(int)); if(NULL != ptr)// Judge ptr Is the pointer null { int i = 0; for(i=0; i<num; i++) { *(ptr+i) = 0; } } free(ptr);// Release ptr The dynamic memory pointed to ptr = NULL;// Is it necessary to ? return 0; }Finally, put the pointer ptr Into a NULL highly necessary , Otherwise, it will cause wild pointer .
calloc function

- calloc The return value of void*, Parameters are two unsigned integers ,num It means that you need to open up several units of space , and size Indicates the size of unit space , Unit is byte , And initialize each byte of the space to 0.; for example , I want to open up 40 Bytes of space , May make num=10,size=4, The size of the space opened up is equal to num*size.
- malloc and calloc The difference is that ,calloc While opening up space , Also initialize each byte of the requested space to zero .
- Other functions and malloc Almost the same

realloc function

Parameters in ptr The pointer points to Memory address to adjust ,
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 :
- 1. The original space is big enough
- 2. There is not enough space after the original space :

situation 1When it's the case 1 When , To expand memory, add space directly after the original memory , The original spatial data does not change .situation 2When it's the case 2 When , When there is not enough space after the original space , The way to expand is : Find another suitable size on the heap spaceContinuous space to use . This function returns a new memory address .Because of the above two situations ,realloc We should pay attention to the use of functions
边栏推荐
- Kubernetes技术与架构(六)
- C語言基礎知識梳理(一)
- Solve the Chinese garbled code of post request and get request in servlet
- 【信息系统项目管理师】第六章 复盘进度管理知识架构
- Redis source code and design analysis -- 13. Ordered collection objects
- 资源池以及资源池化是什么意思?
- China Economic Net: "Yuan universe" is hot
- Naming rules of MySQL database table names -- convenient for automatic conversion tools
- Redis源码与设计剖析 -- 10.列表对象
- C语言n番战--共用体和枚举(八)
猜你喜欢

FPGA——SPI总线控制flash(2)(含代码)

PXE remote installation and kickstart unattended installation technical documents

C语言n番战--链表(九)

SVG、canvas、绘制线段和填充多边形、矩形、曲线的绘制和填充

The 12th Blue Bridge Cup embedded design and development project

UNITY VFX syntax error: unexpected token ‘#‘ at kernel CSMain

【Unity】AVPro使用踩坑,编辑器模式使用视频播放正常,打包后视频无法播放的问题

Understand asp Net core - Cookie based authentication

Redis source code and design analysis -- 10. List object

C language n battle -- structure (VII)
随机推荐
PXE remote installation and kickstart unattended installation technical documents
【社媒营销】出海新思路:Whatsapp Business替代Facebook
FPGA - SPI bus control flash (2) (including code)
Mysql的索引为什么用的是B+树?
Redis source code and design analysis -- 12. Collection objects
【Unity】AVPro使用踩坑,编辑器模式使用视频播放正常,打包后视频无法播放的问题
300 题 第六讲 二次型
XSSGAME小游戏(XSS学习)level1-15
20.有效的括号
PMP practice once a day | don't get lost in the exam -7.22
[unity daily bug] unity reports an unexpected character '‘
An analysis of the CPU explosion of an intelligent transportation background service in.Net
TS类型体操 之 中级类型体操挑战收官之战
[unity] avpro uses stepping pits, and the editor mode uses video playback. The video cannot be played after packaging
全局事件总线
Leetcode skimming -- bit by bit record 022
SVG、canvas、绘制线段和填充多边形、矩形、曲线的绘制和填充
PXE远程安装和Kickstart无人值守安装 技术文档
MySQL log understanding
C language n battle -- structure (VII)