数组输出具有垃圾内存值
c++ Array output has trash memory values
我在c++中对数组进行练习,我得到了垃圾内存值,我该如何修复它呢?
#include<cstdlib>
#include<fsream>
#include<iostream>
#include<locale.h>
#include<string.h>
using namespace std;
string sone;
string stwo;
string sthree;
string sfour;
int one;
int two;
int three;
int four;
int main(int org, char** argv)
{
char array = { 'a', 'b', 'c', 'd' };
while (four <= 10)
{
three = 0
while (three <= 10)
{
two = 0
while (two <= 10)
{
one = 0
while (one <= 10)
{
setlocale(LC_CTYPE, "Local");
ofstream out("file.txt", ios::app);
sone = array[one];
stwo = array[two];
sthree = array[three];
sfour = array[four];
out << sfour + sthree + stwo + sone;
out << "n";
out.close();
one++;
}
two++;
}
four++;
}
five++;
}
}
输出:
1-aaaa
2-aaab
3-aaac
4-aaad
5-aaa#
6-aaa?
7-aaa0
8-aaa#
9-aaa'
10-aaa_
11-aaa?
12-aaba
13-aabb
变量one,two,three,four的取值范围是1到10。而你的数组只有4个字符。您正在访问"数组越界"
似乎你从来没有设置你的into值4等于任何东西。每次调用4++时,它都会抓取一个随机值。
另外,我不确定你是如何编译的,因为没有变量5,你正在增加它。
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- C++具有模板成员变量的类. 和参数内存输出
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 重载 ostream << 运算符,指针作为参数,导致输出上的内存地址
- 为什么cout输出内存地址不是字符串?
- C++可以输出一个结构的内存地址吗
- 提升进程间共享内存删除、权限和输出文件
- 为什么 du -sh 输出错误大小的内存映射文件
- 如果关闭娱乐后()所有内存都会被交易,则如何打印输出
- 获取“将返回值分配给新变量”的警告和输出是内存地址
- 插入std :: basic_ostream实例化输出时的内存故障
- 了解Microsoft的内存泄漏检测输出
- 更新:正在输出内存位置,而不是所需的字符串
- Valgrind 在主机名命令输出中显示内存泄漏
- 怪异的结果从NVPROF输出计算内存带宽
- 从数组中删除未使用的内存,以免输出随机数
- 对数组,乱码输出(未分配的内存?
- std::dec仍然将内存地址输出为十六进制
- 与实际内存内容相比,GDB 内存检查输出偏差 8 个字节
- 是否可以从C++释放分配的内存输出