当前位置:网站首页>Introduction to dynamic memory functions (malloc free calloc realloc)
Introduction to dynamic memory functions (malloc free calloc realloc)
2022-07-27 05:04:00 【~Xiao Ming learns programming】
author :~ Xiao Ming learns programming
Article column :C Basic knowledge of language
All you can see is memories , What you want is the past
Preface
When we write code, we will encounter the following situations : For example, when we use arrays, we will open up a space
int arr[10]={
0};
In this way, we have opened up a piece 40 Bytes of space , But when we want to use 50 even to the extent that 60 What should I do with a space of bytes ? That we use a lot C95 Under the version , The open space in the array is fixed , Because we need to pass in constants, we can't flexibly open up space , So we must use dynamic memory management , Today I will introduce some functions related to dynamic memory development .
malloc

Let's first introduce this function :
This function applies to memory for a contiguous block of free space , And return the pointer to this space .
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 , The user will decide when using it .
If parameters size by 0,malloc The standard is undefined , Depends on the compiler .
free
C Language provides another function free, It is specially used for dynamic memory release and recovery , The function prototype is as follows :
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 .
The following is a code demonstration
#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;
}
calloc

C The language also provides a function called calloc , calloc Function is also used for dynamic memory allocation .
- The function is for num Size is size The elements of open up a space , And initialize each byte of the space to 0.
- And functions malloc The only difference is calloc Initializes each byte of the requested space to full before returning the address 0.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)calloc(10, sizeof(int));
if(NULL != p)
{
// Use space
}
free(p);
p = NULL;
return 0; }
So how do we request initialization for the contents of the requested memory space , So it's easy to use calloc Function to complete the task .
realloc

realloc Functions make dynamic memory management more flexible .
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 be used to adjust the dynamic memory 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
situation 1
When it's the case 1 When , To expand memory, add space directly after the original memory , The original spatial data does not change .
situation 2
When 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 space
Continuous 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
#include <stdio.h>
int main()
{
int *ptr = (int*)malloc(100);
if(ptr != NULL)
{
// Business processing
}
else
{
exit(EXIT_FAILURE);
}
// Expand capacity
// Code 1
ptr = (int*)realloc(ptr, 1000);// Is this ok? ?( What happens if the application fails ?)
// Code 2
int*p = NULL;
p = realloc(ptr, 1000);
if(p != NULL)
{
ptr = p;
}
// Business processing
free(ptr);
return 0; }
边栏推荐
- C language address book management system (linked list, segmented storage of mobile phone numbers, TXT file access, complete source code)
- 节流函数的demo——正则表达式匹配
- The digital China Construction Summit ended with a list of massive pictures on site!
- 深入 Qt5 信号槽新语法
- Jmeter 界面如何汉化?
- kali系统arp介绍(断网嗅探密码抓包)
- HCIA static routing basic simulation experiment
- "Photoshop2021 tutorial" adjust the picture to different aspect ratio
- 二、MySQL高级
- 「Photoshop2021入门教程」对齐与分布制作波点图案
猜你喜欢

STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding

How to import PS style? Photoshop style import tutorial

"Photoshop2021 tutorial" align and distribute to make dot patterns

会议OA之我的审批

日落红暖色调调色滤镜luts预设Sunset LUTs 1

网络协议详解:IP

Standard dialog qmessagebox

Introduction to Web Framework

集成开发环境Pycharm的安装及模板设置

Why is count (*) slow
随机推荐
网络协议详解:IP
Counting Nodes in a Binary Search Tree
HCIA dynamic routing rip basic experiment
Dialog introduction
使用ngrok做内网穿透
【搜索】—— 多源BFS + 最小步数模型
Acceptance and neglect of events
strlen和sizeof的区别
Final Cut Pro中文教程 (2) 素材窗口的认识
标准对话框 QMessageBox
对话框简介
HCIA dynamic routing OSPF experiment
2019 top tennis cup upload
JS tips
日落红暖色调调色滤镜luts预设Sunset LUTs 1
[error reporting] cannot read property 'parsecomponent' of undefined
static和final关键字 学习 demo练习
ps样式如何导入?Photoshop样式导入教程
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
UUID and indexing rules
