当前位置:网站首页>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;
}边栏推荐
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
- 相机内参矩阵K和fov的相互转换
- 微信小程序开发之网络数据请求
- Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
- Network data request for wechat applet development
- University of California | feasible confrontation robust reinforcement learning for unspecified environments
- GBASE 8s UDR内存管理_01_mi_alloc
- 新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析
- Telnet installation and telnet (correct password) cannot log in!
- binarySearch基础二分查找
猜你喜欢
![Scala foundation [set 01]](/img/6b/0f5da7ea923ef3aa436d7af9c4425c.png)
Scala foundation [set 01]

Sccm2012r2 network deployment reinstallation system

Improvement of wechat applet 28 hot search list ①

小程序毕设作品之微信校园维修报修小程序毕业设计成品(1)开发概要

蓝桥杯基础练习——矩阵的回形取数(C语言)

微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建

【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分

Js分页插件支持表格、列表、文本、图像

Flutter 小技巧之优化你使用的 BuildContext

Pymoo learning (6): termination conditions
随机推荐
帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
Improvement of wechat applet 28 hot search list ①
浅谈接口加密
创意下拉多选js插件下载
Selenium 设置元素等待的三种方式详解
Solve the problem that the win10 account has no administrator rights
房地产行业大洗牌
鸿蒙-大喵计算画板-视频
六轴传感器使用学习记录
AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
Imeta | sangerbox: interactive integrated clinical information analysis platform
University of California | feasible confrontation robust reinforcement learning for unspecified environments
Real estate industry reshuffle
安全基础4 ---正则表达式
阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
【刷题记录】21. 合并两个有序链表
【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
Pymoo learning (8):grades
[server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage