#include <iostream>
using namespace std;
const int BUFSIZE = 1 << 20;
char padded_buffer[64 + BUFSIZE + 64];
char* buffer = padded_buffer + 64;
int main()
{
buffer[-1] = '?';
// is that always equivalent to padded_buffer[63] = '?' ?
cout << padded_buffer[63] << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有一段像上面这样的代码。基本上,由于某些原因,我需要“隔离”阵列的两侧。
但我想知道上面的语法是否安全?我知道负索引通常是未定义的行为,但是这种情况又如何呢?
为什么编译器不阻止我使用大于数组长度减一的整数和负整数对数组进行索引?
允许这样做的理由是什么?