当前位置:网站首页>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;
}
边栏推荐
- How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
- D2--FPGA SPI接口通信2022-08-03
- Why is pnpm hitting npm and yarn dimensionality reduction?
- Luogu P3368: 【模板】树状数组 2
- 工程制图知识点
- Support touch screen slider carousel plugin
- Moonbeam团队发布针对整数截断漏洞的紧急安全修复
- [Untitled] Long-term recruitment of hardware engineers-Shenzhen Baoan
- 好资料汇总
- sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
猜你喜欢

Three solutions to solve cross-domain in egg framework

ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色

ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果

What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file

MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)

Spark cluster deployment (third bullet)

egg框架中解决跨域的三种方案

Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法

SQL语句查询字段内重复内容,并按重复次数加序号

动态库之间回调函数使用
随机推荐
行走社会100绝招
DPU — 功能特性 — 存储系统的硬件卸载
php向mysql写入数据失败
[Structure internal power practice] Structure memory alignment (1)
Ethernet Principle
pnpm 是凭什么对 npm 和 yarn 降维打击的
Random code generation
EA谈单机游戏:仍是产品组合中极其重要的部分
树状数组模版+例题
Chapter 12 贝叶斯网络
Luogu P1966: [NOIP2013 提高组] 火柴排队 [树状数组+逆序对]
v-if/v-else determines whether to display according to the calculation
What is the connection and difference between software system testing and acceptance testing? Professional software testing solution recommendation
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
512色色谱图
Redis实现分布式锁-原理-问题详解
长期招聘嵌入式开发-深圳宝安
苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
RedisTemplate: 报错template not initialized; call afterPropertiesSet() before using it
[转帖]嫁人一定要嫁工资至少比你高3571.4元的男士