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

这个也不会产生误差。


当前回答

据我所知,这是没有定义的行为。用它运行一个更大的程序,它会在某个地方崩溃。边界检查不是原始数组(甚至std::vector)的一部分。

使用std::vector和std::vector::iterator's来代替,这样你就不用担心了。

编辑:

只是为了好玩,运行这个,看看你多久会崩溃:

int main()
{
   int arr[1];

   for (int i = 0; i != 100000; i++)
   {
       arr[i] = i;
   }

   return 0; //will be lucky to ever reach this
}

Edit2:

不要运行它。

Edit3:

好的,下面是一个关于数组及其与指针关系的快速课程:

当你使用数组索引时,你实际上是在使用一个伪装的指针(称为“引用”),它是自动解引用的。这就是为什么数组[1]自动返回该下标处的值而不是*(array+1)。

当你有一个指向数组的指针时,像这样:

int arr[5];
int *ptr = arr;

然后,第二个声明中的“array”实际上衰减为指向第一个数组的指针。这和下面的行为是等价的:

int *ptr = &arr[0];

当你试图访问超出你所分配的内存时,你实际上只是使用了一个指向其他内存的指针(这一点c++不会抱怨)。以我上面的示例程序为例,这相当于:

int main()
{
   int arr[1];
   int *ptr = arr;

   for (int i = 0; i != 100000; i++, ptr++)
   {
       *ptr++ = i;
   }

   return 0; //will be lucky to ever reach this
}

编译器不会抱怨,因为在编程中,你经常需要与其他程序通信,尤其是操作系统。这是通过指针完成的。

其他回答

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

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

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

Valgrind将报告一个错误。

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

编辑:

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

Hint

如果你想要快速约束大小数组的范围错误检查,尝试使用boost::array,(也std::tr1::array from <tr1/array>,它将是下一个c++规范的标准容器)。它比std::vector快得多。它在堆上或类实例内部保留内存,就像int array[]一样。 这是一个简单的示例代码:

#include <iostream>
#include <boost/array.hpp>
int main()
{
    boost::array<int,2> array;
    array.at(0) = 1; // checking index is inside range
    array[1] = 2;    // no error check, as fast as int array[2];
    try
    {
       // index is inside range
       std::cout << "array.at(0) = " << array.at(0) << std::endl;

       // index is outside range, throwing exception
       std::cout << "array.at(2) = " << array.at(2) << std::endl; 

       // never comes here
       std::cout << "array.at(1) = " << array.at(1) << std::endl;  
    }
    catch(const std::out_of_range& r)
    {
        std::cout << "Something goes wrong: " << r.what() << std::endl;
    }
    return 0;
}

这个程序将打印:

array.at(0) = 1
Something goes wrong: array<>: index out of range

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

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.