当前位置:网站首页>C语言:动态内存函数
C语言:动态内存函数
2022-07-27 14:27:00 【发发是只呆头鹅】
1、动态内存函数
1.1 malloc和free
malloc这个函数向内存申请一块连续可用的空间,并返回指向这块空间的指针。![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tjBAy9lP-1632745217929)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927191647836.png)]](/img/d6/c444959cbdbc2005db23d48950072c.png)
参数为开辟字节的大小,返回类型为void*,空间开辟成功指针指向该空间的地址,开辟失败后指针为NULL,所以每次开辟空间后都需要判断是否开辟成功,我们来看一个例子:
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(10 * sizeof(int));
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i < 10; i++)
{
*(ptr + i) = 0;
}
}
for (int i = 0; i < 10; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WfH1UIUx-1632745217931)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193142358.png)]](/img/64/6c43fc5944dc9f4fde4e12dafef86a.png)
开辟空间后,到最后要free开辟的空间,还要将其职位NULL。
1.2 calloc
calloc 函数也用来动态内存分配,函数的功能是为 num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GdlR4rkB-1632745217932)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193642191.png)]](/img/5e/ecdbbf3d6fa056f7035e95aacd7db8.png)
其与malloc的区别就是calloc会将开辟好的空间的元素初始化赋值为0,我们来看一个例子:
int main() {
int* ptr = NULL;
ptr = (int*)calloc(10, sizeof(int));
for (int i = 0; i < 10; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FcFYHTzu-1632745217933)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193928537.png)]](/img/f9/512993a59d88b96cb3931637f3c7b3.png)
1.3 realloc
realloc函数的出现让动态内存管理更加灵活,有时会我们发现过去申请的空间太小了,有时候我们又会觉得申请的空间过大了,那为了合理的时 候内存,我们一定会对内存的大小做灵活的调整。那 realloc 函数就可以做到对动态开辟内存大 小的调整。
memblock为需要调整大小的位置,size为调整后的新大小,返回值为调整之后的内存起始位置。我们来看一个例子:
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(10 * sizeof(int));
if (NULL != ptr){
//判断ptr指针是否为空
for (int i = 0; i < 10; i++){
*(ptr + i) = i;
}
}
for (int i = 0; i < 10; i++){
printf("%d ", *(ptr + i));
}
int* p = NULL;
p = realloc(ptr, 20 * sizeof(20)); //将空间调整为20个int类型的大小
if (NULL != p) {
ptr = p;
for (int i = 10; i < 20; i++)
*(ptr + i) = i;
}
for (int i = 0; i < 20; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}

realloc在扩大空间时会遇到两种情况,
(1)原来空间后面的空间足够,直接在原来的空间后面开始扩容。
(2)原来空间后面的空间不够,寻找一块新的空间,并将原来空间中的值赋到新的空间中,并且释放原来的空间。
2、柔性数组
结构中的最后一个元素允许是未知大小的数组,这就叫做柔性数组成员。
2.1 柔性数组的特点:
- 结构中的柔性数组成员前面必须至少一个其他成员。
- sizeof 返回的这种结构大小不包括柔性数组的内存。
- 包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
2.2 柔性数组的使用
来看代码:
struct soft_arry {
int i;
int arr[];
}*p;
int main() {
struct soft_arry* p = (struct soft_arrya*)malloc(sizeof(struct soft_arry) + 100 * sizeof(int));
//开辟的空间使得arr数组可以容纳下100个int类型的数据
if(p != NULL){
for (int i = 0; i < 100; i++) {
p->arr[i] = i;
}
for (int i = 0; i < 100; i++) {
printf("%d ", p->arr[i]);
}
}
free(p);
p = NULL;
return 0;
}

边栏推荐
- [daily question 1] 558. Intersection of quadtrees
- 3.3-5v conversion
- How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
- The design method of integral operation circuit is introduced in detail
- Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
- Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
- 复杂度分析
- Summer Challenge harmonyos realizes a hand-painted board
- Dan bin Investment Summit: on the importance of asset management!
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
猜你喜欢

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找

MLX90640 红外热成像仪测温传感器模块开发笔记(七)

STL value string learning

Leetcode 190. reverse binary bit operation /easy

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

reflex

QT (five) meta object properties

【剑指offer】面试题46:把数字翻译成字符串——动态规划

Jump to the specified position when video continues playing

Dan bin Investment Summit: on the importance of asset management!
随机推荐
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
EMC design scheme of CAN bus
Several basic uses of tl431-2.5v voltage reference chip
Leetcode 191. number of 1 bits bit operation /easy
Jump to the specified position when video continues playing
3.3-5v conversion
$router.back(-1)
HJ8 合并表记录
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
JUC(JMM、Volatile)
Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
Spark TroubleShooting整理
Dan bin Investment Summit: on the importance of asset management!
Spark3中Catalog组件设计和自定义扩展Catalog实现
使用双星号代替Math.pow()
Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
C:什么是函数中的返回值(转)
Summer Challenge harmonyos realizes a hand-painted board
Network equipment hard core technology insider router Chapter 21 reconfigurable router