[英]Array printing out -858993460
I've been trying to work out why my array is printing out -858993460 with the following code我一直在尝试找出为什么我的数组使用以下代码打印出 -858993460
int myInt = 0;
int myArray[10]; // I know this is undefined, but the same results occur if I put {} to initialize it
while(myInt <= 10)
{
myArray[myInt] = myInt;
myInt++;
std::cout << "Test " << myArray[myInt] << std::endl;
std::cout << "Counter " << myInt << std::endl;
}
When print this out, myInt
increments fine.打印出来时,
myInt
递增。 However, myArray
is printing out -858993460.但是,
myArray
打印出 -858993460。 If I insert a break point and step over the while loop with each iteration, I can see the numbers are being fed into the array, but it only prints out that random number (assuming it's a random number from the stack/heap?).如果我在每次迭代时插入一个断点并跨过 while 循环,我可以看到数字被输入到数组中,但它只打印出那个随机数(假设它是来自堆栈/堆的随机数?)。
Now, if I swap around the while loop so it's now现在,如果我在 while 循环周围交换,那么现在
while(myInt <= 10)
{
myInt++;
myArray[myInt] = myInt;
}
it prints out the numbers correctly.它正确打印出数字。 I can't seem to work out what's going on here...
我似乎无法弄清楚这里发生了什么......
Arrays in C (and by extension C++) are zero based , so an array of ten elements is indexed 0
through 9
. C(以及扩展的 C++)中的数组是从零开始的,因此一个包含十个元素的数组的索引
0
到9
。
But your loop is trying to access elements 0
through 10
.但是您的循环正在尝试访问元素
0
到10
。 myArray[10]
is actually the eleventh element of the array, which doesn't actually exist. myArray[10]
实际上是数组的第十一个元素,实际上并不存在。 Trying to change it is undefined behavior.试图改变它是未定义的行为。 Your original code was actually closer to being correct;
您的原始代码实际上更接近正确; your revised version only made it worse.
你的修订版只会让情况变得更糟。 What you want is:
你想要的是:
int myInt = 0;
int myArray[10];
while(myInt < 10) // <- strictly LESS THAN
{
myArray[myInt] = myInt;
++myInt; // <- increment AFTER accessing/assigning
}
In comments, you added an additional requirement that myArray[0]
should contain 1
and so forth.在评论中,您添加了一个附加要求,即
myArray[0]
应包含1
等等。 To get this, you need to access the element before modifying the index.为此,您需要在修改索引之前访问该元素。 You could do this with two variables:
您可以使用两个变量来执行此操作:
int myInt = 0;
int myArray[10];
while(myInt < 10)
{
myArray[myInt] = myInt+1;
++myInt;
}
You can't combine the two , unfortunately:不幸的是,您不能将两者结合起来:
int myInt = 0;
int myArray[10];
while(myInt < 10)
{
myArray[myInt] = ++myInt; // <- NOPE: wrong order
}
In the revised code where you try to print out the values, you tried to access myArray[MyInt]
after you changed MyInt
.在您尝试打印出值的修改后的代码中,您在更改
MyInt
后尝试访问myArray[MyInt]
。 So you're now looking at a different element than the one you just set.因此,您现在正在查看与您刚刚设置的元素不同的元素。 No surprise it didn't print out what you want!
毫不奇怪,它没有打印出你想要的东西! Just move the increment to the end:
只需将增量移到末尾:
while(myInt <= 10)
{
myArray[myInt] = myInt+1;
std::cout << "myArray [" << myInt << "] = " << myArray[myInt] << '\n';
++myInt; <- ALWAYS INCREMENT THE LOOP INDEX LAST
}
You print myArray[myInt]
after you have incremented myInt
.增加
myInt
后打印myArray[myInt]
。 That array element hasn't been initialized yet.该数组元素尚未初始化。
More over at the end myInt
is 10 and your myArray
only goes from 0
to 9
leading to a buffer overflow.最后
myInt
是 10 并且您的myArray
仅从0
变为9
导致缓冲区溢出。
And last while(myInt <= 10)
goes even one step further, initializing myArray[10]
and printing myArray[11]
, both outside the array.最后
while(myInt <= 10)
更进一步,初始化myArray[10]
并打印myArray[11]
,都在数组之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.