我在一个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;

这个也不会产生误差。


当前回答

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 地址以外的地址 分配的缓冲区。

其他回答

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 地址以外的地址 分配的缓冲区。

据我所知,局部变量是在堆栈上分配的,所以在你自己的堆栈上越界只能覆盖一些其他的局部变量,除非你oob太多,超过了你的堆栈大小。 由于在函数中没有声明其他变量,因此不会产生任何副作用。尝试在第一个变量/数组之后声明另一个变量/数组,看看会发生什么。

libstdc++是gcc的一部分,它有一个用于错误检查的特殊调试模式。它由编译器标志-D_GLIBCXX_DEBUG启用。其中,它以性能为代价对std::vector进行边界检查。这是gcc最新版本的在线演示。

因此,实际上您可以使用libstdc++调试模式进行边界检查,但您应该只在测试时执行,因为与正常的libstdc++模式相比,它的性能损失显著。

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.

一个很好的方法,我经常看到,我已经使用实际上是注入一些NULL类型的元素(或一个创建的,像uint THIS_IS_INFINITY = 82862863263;)在数组的末尾。

然后在循环条件检查时,TYPE *pagesWords是某种指针数组:

int pagesWordsLength = sizeof(pagesWords) / sizeof(pagesWords[0]);

realloc (pagesWords, sizeof(pagesWords[0]) * (pagesWordsLength + 1);

pagesWords[pagesWordsLength] = MY_NULL;

for (uint i = 0; i < 1000; i++)
{
  if (pagesWords[i] == MY_NULL)
  {
    break;
  }
}

如果数组中填充了结构类型,则此解决方案不会生效。