当前位置:网站首页>C语言学习日记3——realloc函数
C语言学习日记3——realloc函数
2022-07-25 19:31:00 【herb.dr】
目录
一、小扩容
1.1 代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* arr1 = (int*)malloc(10 * 4);
for (int i = 0; i < 10; i++) {
arr1[i] = i;
}
for (int i = 0; i < 10; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
int* arr2 = (int*)realloc(arr1, 20 * 4);
for (int i = 0; i < 20; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
printf("%p %p\n", arr1, arr2);
return 0;
}1.2 结果
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 2080374908 47324 -457679488 407 -457703088 407 1174430068 1113543504 1398230866 1096766021
00000197e4b813d0 00000197e4b813d0
二、大扩容
2.1 代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* arr1 = (int*)malloc(10 * 4);
for (int i = 0; i < 10; i++) {
arr1[i] = i;
}
printf("\n");
int* arr2 = (int*)realloc(arr1, 100000 * 4);
printf("%p %p\n", arr1, arr2);
return 0;
}2.2 结果
0000023f788313d0 0000023f78a10080
2.3 讲解
小扩容在原来的基础上增加内存
大扩容是删掉了原来的内存重新分配了一块内存,建议是不要用原来的指针操作内存

我们可以用下面的形式
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* arr1 = (int*)malloc(10 * 4);
for (int i = 0; i < 10; i++) {
arr1[i] = i;
}
printf("\n");
arr1 = (int*)realloc(arr1, 100000 * 4);
printf("%p\n", arr1);
return 0;
}边栏推荐
- ML的编程技巧:
- Wechat applet 10 - wechat template
- balanced binary tree
- [Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
- 帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
- Imperial cms7.5 imitation "question and answer library" question and answer learning platform website source code with mobile version
- A good way to generate interface documents efficiently
- Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
- 给容器添加3d效果的副标题
- [applet development] do you know about applet development?
猜你喜欢

某公司网络设计与规划

C# 合并集合

Code sharing of social chat platform developed by dating website (III)

微信小程序 29 热搜榜的完善②

JS learning notes 17: DOM query exercise

给容器添加3d效果的副标题

Modelsim and quartus jointly simulate PLL FIFO and other IP cores

Alibaba cloud free SSL certificate application detailed process

由一个蓝桥杯基础题报时助手而引出的常见误区

安全基础4 ---正则表达式
随机推荐
GBASE 8s UDR内存管理_01_mi_alloc
[record of question brushing] 21. Merge two ordered linked lists
Sccm2012r2 network deployment reinstallation system
Binary tree visualization
授权无线通信标准
Flutter tips: optimizing the buildcontext you use
Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
Improvement of wechat applet 26 playing music page ②
新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析
Gbase 8s UDR memory management_ 02_ mi_ dalloc
平衡二叉树
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
Grid layout frequently asked interview questions
How to ensure the consistency of double write between database and cache?
微信小程序 26 播放音乐页的完善②
An idea of solving div adapting to screen
Swift 基础 Codable(JSONEncoder JSONDecoder)的使用
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
浅谈接口加密
Hongke shares | how to solve blackmail software security vulnerabilities