当前位置:网站首页>C language learning diary 3 - realloc function
C language learning diary 3 - realloc function
2022-07-25 19:33:00 【herb.dr】
Catalog
Two 、 Large capacity expansion
One 、 Small expansion
1.1 Code
#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 result
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
Two 、 Large capacity expansion
2.1 Code
#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 result
0000023f788313d0 0000023f78a10080
2.3 Explain
Small expansion increases memory on the original basis
Large expansion is to delete the original memory and reallocate a piece of memory , The suggestion is not to use the original pointer to operate the memory

We can use the following form
#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;
}边栏推荐
- 六轴传感器使用学习记录
- SDL text display
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
- C语言学习日记3——realloc函数
- Sccm2012r2 network deployment reinstallation system
- 微信小程序 26 播放音乐页的完善②
- Flutter tips: optimizing the buildcontext you use
- ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
- 微信小程序10-微搭模板
- 由一个蓝桥杯基础题报时助手而引出的常见误区
猜你喜欢

Improvement of wechat applet 26 playing music page ②

Network design and planning of a company

Nezha d1-h test microbench

Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks

小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告

微信小程序开发之WXSS模板样式与WXS脚本语言
![[hdlbits questions] Verilog language (3) modules: hierarchy section](/img/35/ccdbb55aa0aff7e9dec2bf9e64c4e2.png)
[hdlbits questions] Verilog language (3) modules: hierarchy section

某公司网络设计与规划

IP地址的概念

Illegal mix of collations for operation ‘UNION‘(bug记录)
随机推荐
[record of question brushing] 21. Merge two ordered linked lists
鸿蒙-大喵计算画板-简介
Talk about 11 tips for interface performance optimization
C merge set
[wp]ctfshow-web入门信息搜集
Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
Day7:有序二叉树(二叉搜索树)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(1)开发概要
平衡二叉树
Sccm2012r2 network deployment reinstallation system
六轴传感器使用学习记录
某公司网络设计与规划
Hash undirected graph visualization
AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
[CSAPP Practice Problem 2.32] tsub_ OK (int x, int y) judge whether complement subtraction overflows
GBASE 8s UDR内存管理_01_mi_alloc
Add a subtitle of 3D effect to the container
【好书推荐】-- 《以太网权威指南》(第2版)
[CSAPP Practice Problem 2.32] tsub_ok(int x, int y)判断补码减法是否溢出
微信小程序开发之网络数据请求