当前位置:网站首页>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;
}
边栏推荐
- Iptables implementation under the network limited (NTP) synchronization time custom port
- TensorFlow installation steps
- SQL SERVER on master-slave table trigger design
- k-nearest neighbor fault monitoring based on multi-block information extraction and Mahalanobis distance
- Luogu: P2574 XOR的艺术 [线段树]
- 网络安全研究发现,P2E项目遭遇黑客攻击只是时间问题
- Chapter 12 Bayesian Networks
- The color of life divine
- Thinking after writing a code with a very high CPU usage
- SVG Star Wars Style Toggle Toggle Button
猜你喜欢
网页直接访问链接不让安全中心拦截
DNS 查询原理详解
Adb authorization process analysis
MVCC of Google's Fragmented Notes (Draft)
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
TensorFlow installation steps
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
Ethernet Principle
随机推荐
工程制图试题
The difference between beautiful MM and ordinary MM
DPU — 功能特性 — 安全系统的硬件卸载
512-color chromatogram
How Entrepreneurs Attract Venture Capitalists
php向mysql写入数据失败
JS syntax usage
树状数组模版+例题
Spark cluster deployment (third bullet)
spark集群部署(第三弹)
写出了一个CPU占用极高的代码后引发的思考
Why is pnpm hitting npm and yarn dimensionality reduction?
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
unity 头发的渲染
unity urp 渲染管线顶点偏移的实现
【结构体内功修炼】枚举和联合的奥秘(三)
星座理想情人
达梦数据库大表添加字段
【无标题】长期招聘硬件工程师-深圳宝安
【结构体内功修炼】结构体内存对齐(一)