当前位置:网站首页>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;
}
边栏推荐
- Random code generation
- tear apart loneliness
- 宝塔实测-搭建中小型民宿酒店管理源码
- What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
- Chapter 12 贝叶斯网络
- ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
- The magic weapon for small entrepreneurs!
- [Structure internal power practice] Structure memory alignment (1)
- TensorFlow安装步骤
- 路由----router
猜你喜欢
![[Structural Internal Power Cultivation] Structural Realization Stages (2)](/img/eb/c80e12edbf4a411227be7e33096ed3.png)
[Structural Internal Power Cultivation] Structural Realization Stages (2)

spark集群部署(第三弹)

SVG Star Wars Style Toggle Toggle Button

ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
![[Structure internal power practice] Structure memory alignment (1)](/img/31/4ddc16810da8238ac95a93d007e12e.png)
[Structure internal power practice] Structure memory alignment (1)

学习笔记14--机器学习在局部路径规划中的应用

XSS靶机通关以及XSS介绍

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

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

【结构体内功修炼】结构体内存对齐(一)
随机推荐
[转帖]嫁人一定要嫁工资至少比你高3571.4元的男士
Chapter 12 贝叶斯网络
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
unity 头发的渲染
剑指Offer面试题解总结1-10
DPU — 功能特性 — 管理系统的硬件卸载
撕裂寂寞
RedisTemplate: 报错template not initialized; call afterPropertiesSet() before using it
The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
动态库之间回调函数使用
学习笔记14--机器学习在局部路径规划中的应用
P1103 书本整理
程序设计中的感悟
按钮上显示值的轮流切换
SQL SERVER on master-slave table trigger design
SQL语句查询字段内重复内容,并按重复次数加序号
Green Apple Forum reopens
路由----router
软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐
[Structural Internal Power Cultivation] Structural Realization Stages (2)