当前位置:网站首页>Dynamic memory development (C language)
Dynamic memory development (C language)
2022-08-05 08:38:00 【InfoQ】
three regions of memory

Why use dynamic memory development
动态内存分配的函数
free
free(p);
p=NULL;
malloc
void* malloc (size_t size);打印错误原因的一种方式
#include<string.h>
#include<errno.h>
#include<stdio.h>
printf("%s\n",strerror(errno));
由malloc和free的实例演示
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
int main()
{
int* p = (int*)malloc(10 * sizeof(int));
if (p == NULL)
printf("%s\n", strerror(errno));
else
{
int i = 0;
for (i = 0; i < 10; i++)
{
*(p + i) = i;
}
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
}
free(p);
p = NULL;
return 0;
}

realloc
int* p = (int*)malloc(20);
int* p2 = realloc(p, 40);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
int main()
{
int* p = (int*)malloc(20);
if (p == NULL)
{
printf("%s\n", strerror(errno));
}
else
{
int i = 0;
for (i = 0; i < 5; i++)
{
*(p + i) = i;
}
}
int* p2 = realloc(p, 40);
if (p2 != NULL)
{
p = p2; //把p2The first address was given againp,Connected
int i = 0;
for (i = 5; i < 10; i++)
{
*(p + i) = i;
}
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
}
free(p);
p = NULL;
return 0;
}
边栏推荐
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
- How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
- 星座理想情人
- JVM运行流程,运行时数据区,类加载,垃圾回收,JMM解析
- 哪个是你爱情的颜色?
- 创业者如何吸引风险投资商
- Luogu P1908: 逆序对 [树状数组]
- SQL SERVER on master-slave table trigger design
- 七夕看什么电影好?爬取电影评分并存入csv文件
- routing----router
猜你喜欢

ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感

网页直接访问链接不让安全中心拦截

嵌入式系统:基本定时器

宝塔实测-搭建中小型民宿酒店管理源码

v-if/v-else determines whether to display according to the calculation

How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect

How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
![[Structure internal power practice] Structure memory alignment (1)](/img/31/4ddc16810da8238ac95a93d007e12e.png)
[Structure internal power practice] Structure memory alignment (1)

国际原子能机构总干事称乌克兰扎波罗热核电站安全形势堪忧

Fiddler工具讲解
随机推荐
Constellation ideal lover
【无标题】长期招聘硬件工程师-深圳宝安
十一道家常小菜详细攻略[图文并茂]
Adb 授权过程分析
SQL语句查询字段内重复内容,并按重复次数加序号
[Untitled] Long-term recruitment of hardware engineers-Shenzhen Baoan
CROS and JSONP configuration
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
JS syntax usage
Random code generation
RedisTemplate: 报错template not initialized; call afterPropertiesSet() before using it
SVG big fish eat small fish animation js special effects
Luogu P1966: [NOIP2013 提高组] 火柴排队 [树状数组+逆序对]
Controlling number and letter input in ASP
sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
RedisTemplate: error template not initialized; call afterPropertiesSet() before using it
力扣刷题八月第一天
让程序员崩溃的N个瞬间(非程序员误入)
Fiddler工具讲解