当前位置:网站首页>动态内存开辟(C语言)
动态内存开辟(C语言)
2022-08-05 08:33:00 【InfoQ】
内存的三个区域

为什么要使用动态内存开辟
动态内存分配的函数
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; //把p2的首地址又给了p,做到了衔接
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;
}
边栏推荐
- uniapp time component encapsulates year-month-day-hour-minute-second
- 宝塔实测-搭建中小型民宿酒店管理源码
- Iptables implementation under the network limited (NTP) synchronization time custom port
- Thinking after writing a code with a very high CPU usage
- 达梦数据库大表添加字段
- k-nearest neighbor fault monitoring based on multi-block information extraction and Mahalanobis distance
- DPU — 功能特性 — 安全系统的硬件卸载
- Qt writes custom controls: one of the text spotlight effects
- P1103 书本整理
- 【结构体内功修炼】结构体内存对齐(一)
猜你喜欢

Three solutions to solve cross-domain in egg framework
![[Structural Internal Power Cultivation] Structural Realization Stages (2)](/img/eb/c80e12edbf4a411227be7e33096ed3.png)
[Structural Internal Power Cultivation] Structural Realization Stages (2)

MVCC of Google's Fragmented Notes (Draft)

php fails to write data to mysql

How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color

Ethernet Principle

sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?

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

谷歌零碎笔记之MVCC(草稿)

力扣刷题八月第一天
随机推荐
执子之手,与子偕老。你同意么?
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
长期招聘嵌入式开发-深圳宝安
路由----router
DPU — 功能特性 — 安全系统的硬件卸载
DNS 查询原理详解
爱情是一部忧伤的乐曲
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
How Entrepreneurs Attract Venture Capitalists
Luogu P3368: 【模板】树状数组 2
[Structure internal power practice] Structure memory alignment (1)
SVG big fish eat small fish animation js special effects
unity urp 渲染管线顶点偏移的实现
Jmeter永久设置中文界面
[Structural Internal Power Cultivation] Structural Realization Stages (2)
D2--FPGA SPI接口通信2022-08-03
The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
SVG Star Wars Style Toggle Toggle Button
v-if/v-else determines whether to display according to the calculation
七夕给自己new一个chatRobot当对象