当前位置:网站首页>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;
}
边栏推荐
- 国际原子能机构总干事称乌克兰扎波罗热核电站安全形势堪忧
- Jmeter永久设置中文界面
- MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
- k-nearest neighbor fault monitoring based on multi-block information extraction and Mahalanobis distance
- 树状数组模版+例题
- Version number naming convention
- Controlling number and letter input in ASP
- “充钱”也难治快手的“亏亏亏”?
- P1160 队列安排
- The difference between beautiful MM and ordinary MM
猜你喜欢

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

MVCC of Google's Fragmented Notes (Draft)

SVG big fish eat small fish animation js special effects

EA谈单机游戏:仍是产品组合中极其重要的部分

pnpm 是凭什么对 npm 和 yarn 降维打击的

Why is pnpm hitting npm and yarn dimensionality reduction?

【结构体内功修炼】结构体实现位段(二)

php fails to write data to mysql

Redis implements distributed lock-principle-detailed explanation of the problem

mySQL数据库初始化失败,有谁可以指导一下吗
随机推荐
Codeforce 8.1-8.7做题记录
Nn. Unfold and nn. The fold
Qt writes custom controls: one of the text spotlight effects
[Structure internal power practice] Structure memory alignment (1)
Iptables implementation under the network limited (NTP) synchronization time custom port
创业者如何吸引风险投资商
网络安全研究发现,P2E项目遭遇黑客攻击只是时间问题
DTcloud 装饰器
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
512-color chromatogram
手机上流行的各类谜语
8.4 Summary of the mock competition
Pagoda measurement - building small and medium-sized homestay hotel management source code
动态内存开辟(C语言)
力扣每日一题
“充钱”也难治快手的“亏亏亏”?
XSS靶机通关以及XSS介绍
tear apart loneliness
路由----router
软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐