简体   繁体   English

奇怪的数组行为

[英]Strange Array Behaviour

I have this function that takes an array of 512 Vertices. 我有这个函数,它采用512个顶点的数组。 (each one containing x,y,z coordinates). (每个包含x,y,z坐标)。 Anyway, I made a mistake and instead of accessing the array 512 times, I did it 513 times. 无论如何,我犯了一个错误,而不是访问阵列512次,我做了513次。 Instead of "zeros" I got a number. 而不是“零”我得到了一个数字。 I run it again, the same. 我再次运行它,同样的。 I increased the iterations and the same thing again. 我再次增加了迭代次数。 Even though I was going beyond the limits of the array random values were showing up each time I the function. 即使我超出了数组的限制,每次我的函数都会出现随机值。 What are those values? 那些价值观是什么? Am I accessing anything in the OS? 我是否在操作系统中访问任何内容? (it may sound stupid but im new to C++ and pointers) (这可能听起来很愚蠢,但对C ++和指针来说是新手)

void print_facet_array(FACET3 *f)
{
    int i=0;
    for (i=0;i<=513;i++)
    {
        printf("The vertices (x,y,z) for facet %d are: V_1 =  x:%f , y:%f, z:%f. \n", i, f[i].p1.x, f[i].p1.y, f[i].p1.z);
        printf("The vertices (x,y,z) for facet %d are: V_2 =  x:%f , y:%f, z:%f. \n", i, f[i].p2.x, f[i].p2.y, f[i].p2.z);
        printf("The vertices (x,y,z) for facet %d are: V_3 =  x:%f , y:%f, z:%f. \n", i, f[i].p3.x, f[i].p3.y, f[i].p3.z);
    }

}

Actually, your loop goes through 514 times. 实际上,你的循环经历了514次。 So you're overrunning by 2. 所以你超过了2。

Overrunning an array in C/C++ is completely undefined behavior. 在C / C ++中覆盖数组是完全未定义的行为。 Anything can happen (crash, bad data). 任何事情都可能发生(崩溃,坏数据)。

In your case, you're probably reading stack or heap garbage - which can be anything. 在你的情况下,你可能正在阅读堆栈或堆垃圾 - 这可能是任何东西。

Accessing an array beyond its bounds results in Undefined Behavior . 访问超出其边界的数组会导致未定义的行为
Undefined Behavior means that anything can happen and all safe bets are off. 未定义的行为意味着任何事情都可能发生,所有安全的赌注都会被取消。

So those values can be anything really, they are values present at some memory location which was not reserved for your array.They might belong to some other valid variable maybe or not. 所以这些值可以是任何真正的值,它们是存在于某个内存位置的值,这些值不是为您的数组保留的。它们可能属于某个其他有效变量,可能与否。

Just remember NEVER to access array elements beyond its bounds. 只记得永远不要访问超出其边界的数组元素。

Since you are starting to learn C++, this should be a good read: 既然你开始学习C ++,那应该是一个很好的阅读:
What every c programmer should know about Undefined Behavior. 每个c程序员应该知道什么是Undefined Behavior。

C and C++ do not support array bounds checking. C和C ++不支持数组边界检查。 Unlike Java, where accessing an array outside of its legal bounds will cause an exception to be shown, C++ allows the access. 与Java不同,访问其合法边界之外的数组将导致显示异常,C ++允许访问。 In short, congratulations; 总之,祝贺; you've created dice (a random-number generator). 你创造了骰子(一个随机数发生器)。

The values you're seeing after you reach the end are whatever happens to be in memory after the last element. 在到达结束之后看到的值是在最后一个元素之后发生在内存中的任何值。 It might be an old variable, another variable, or just garbage from an uninitialized block of memory. 它可能是一个旧变量,另一个变量,或者只是来自未初始化的内存块的垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM