当前位置:网站首页>动态创建数组(c6385 正在从“a”读取无效数据)
动态创建数组(c6385 正在从“a”读取无效数据)
2022-04-22 05:35:00 【lxt1101】
最近在写代码的时候出现一个这样的问题C6385正在从“a”去读无效数据,这里的a是一个数组
int a[maxn];//这一行是在堆区创建的,所以没有初始化也会默认初始化元素全为0.
然后就是在调用数组进行操作和赋值的时候,就出现警告
a[p]=i;
平时好像没遇到这样的问题静态定义直接就能用,今天不知道怎么出现的。
解决这个问题的办法是创建动态数组
创建动态数组的有两种办法c++风格的用new,c语言的风格的用malloc函数
1.new动态创建数组
int *arr=new int[maxn];
同时记得用delete释放数组内存,不然内存没有释放,就会变成内存污染,没有指针指向这块内存,这块内存就不能被别人使用,如果这样的数组多了,你的电脑内存就会越来越少。
在数组使用完后释放内存:
delete []arr;
2.用malloc函数创建内存
int *arr=(int*)malloc(sizeof(int)*n);//给数组arr申请存储空间,空间长度为n
用这种方式好像不用delete去释放内存。
以上两种定义动态内存在使用数组的时候好事像一般数组那样直接使用,在使用的时候并没有什么不同。
版权声明
本文为[lxt1101]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lxt1101/article/details/123703604
边栏推荐
- 5.The Simple Problem
- C language version: establishment and basic operation of chain team
- Error Putty X11 proxy: Authorisation not recognised
- @PostConstruct方法内部死循环引起的问题
- Read write separation of MYCAT
- Homebrew的基本使用与常见异常
- MySQL存储时间的最佳实践
- C language version: dynamic establishment of binary tree
- 2022 Niuke winter vacation supplementary question record 2
- 二叉树五个重要性质(附图理解)
猜你喜欢
随机推荐
Redis real-time synchronization tool recommendation
Force buckle - 354 Russian Doll envelope problem
我常用的开发软件
枚舉和Lambda錶達式
数字三角形(动态规划dp)
机器学习——用鸢尾花数据集画P-R曲线和ROC曲线
C language version: the establishment and basic operation of chain stack
JVM探究
list stream: reduce的使用实例
Write a program to automatically generate the two-color ball number of welfare lottery with MATLAB
1.计算a+b
8.整数分解
Do you know the three implementation methods of strlen?
Mysql基础知识
11.a==b?
Kaggle_ Detailed explanation of NBME NLP competition baseline (2)
Simulate the infectious disease model with MATLAB (only do matlab simulation learning and practice, not actual situation and application)
MySQL Chapter 6 installation and use of Navicat
Error Putty X11 proxy: Authorisation not recognised
MySQL JDBC programming









