我在一个c++程序中分配值,就像这样:

#include <iostream>
using namespace std;
int main()
{
    int array[2];
    array[0] = 1;
    array[1] = 2;
    array[3] = 3;
    array[4] = 4;
    cout << array[3] << endl;
    cout << array[4] << endl;
    return 0;
}

程序输出3和4。这应该是不可能的。我使用g++ 4.3.3

下面是编译和运行命令

$ g++ -W -Wall errorRange.cpp -o errorRange
$ ./errorRange
3
4

只有当分配数组[3000]=3000时,它才会给我一个分割错误。

如果gcc不检查数组边界,我怎么能确定我的程序是正确的,因为它可能导致一些严重的问题以后?

我将上面的代码替换为

vector<int> vint(2);
vint[0] = 0;
vint[1] = 1;
vint[2] = 2;
vint[5] = 5;
cout << vint[2] << endl;
cout << vint[5] << endl;

这个也不会产生误差。


当前回答

The behavior can depend on your system. Typically, you will have a margin for out of bounds, sometimes with value of 0 or garbage values. For the details you can check with memory allocation mechanism used in your OS. On top of that, if you use the programming language like c/c++, it will not check the bounds when you using some containers, like array. So, you will meet "undefined event" because you do not know what the OS did below the surface. But like the programming language Java, it will check the bound. If you step outside of the bound, you will get an exception.

其他回答

当你用int array[2]初始化数组时,为2个整数分配空间;但是标识符数组只是指向该空间的开始。当你访问数组[3]和数组[4]时,如果数组足够长,编译器会简单地增加地址以指向这些值的位置;尝试访问像数组[42]这样的东西,而不首先初始化它,您将最终获得内存中该位置已经存在的任何值。

编辑:

关于指针/数组的更多信息:http://home.netcom.com/~tjensen/ptr/pointers.htm

如果你稍微改变一下程序:

#include <iostream>
using namespace std;
int main()
{
    int array[2];
    INT NOTHING;
    CHAR FOO[4];
    STRCPY(FOO, "BAR");
    array[0] = 1;
    array[1] = 2;
    array[3] = 3;
    array[4] = 4;
    cout << array[3] << endl;
    cout << array[4] << endl;
    COUT << FOO << ENDL;
    return 0;
}

(大写字母的变化——如果你想这么做,就用小写字母。)

您将看到变量foo已被销毁。您的代码将把值存储到不存在的数组[3]和数组[4]中,并能够正确地检索它们,但实际使用的存储将来自foo。

因此,在最初的示例中,您可以“逃避”超出数组的界限,但代价是在其他地方造成损害——这种损害可能很难诊断。

至于为什么没有自动边界检查——一个正确编写的程序不需要它。一旦完成了这一点,就没有理由进行运行时边界检查,这样做只会减慢程序的速度。最好在设计和编码过程中把这些都弄清楚。

c++基于C语言,C语言被设计成尽可能接近汇编语言。

在Valgrind中运行这个,你可能会看到一个错误。

正如Falaina指出的那样,valgrind并没有检测到很多堆栈损坏的实例。我刚刚在valgrind下尝试了样本,它确实报告了零错误。然而,Valgrind可以帮助发现许多其他类型的内存问题,在这种情况下,它不是特别有用,除非你修改你的构建,包括——stack-check选项。如果构建并运行样例为

g++ --stack-check -W -Wall errorRange.cpp -o errorRange
valgrind ./errorRange

Valgrind将报告一个错误。

不明确的行为对你有利。不管你要破坏的是什么内存,显然都没有任何重要的东西。注意,C和c++不对数组进行边界检查,因此在编译或运行时不会捕获类似的东西。

C或c++不会检查数组访问的边界。

您正在堆栈上分配数组。通过数组[3]索引数组等价于*(array + 3),其中array是指向&array[0]的指针。这将导致未定义的行为。

在C语言中捕捉这种情况的一种方法是使用静态检查器,例如splint。如果你运行:

splint +bounds array.c

on,

int main(void)
{
    int array[1];

    array[1] = 1;

    return 0;
}

然后你会得到警告:

数组.c:(在函数main中) array.c:5:9:可能超出范围 存储: 数组[1] 无法解决约束的: 要求0 >= 1 需要满足的先决条件: require maxSet(array @ array.c:5:9) >= 1 地址以外的地址 分配的缓冲区。