相关疑难解决方法(0)

C++ 从数组定义行为中间的指针进行负索引?

#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)

我有一段像上面这样的代码。基本上,由于某些原因,我需要“隔离”阵列的两侧。

但我想知道上面的语法是否安全?我知道负索引通常是未定义的行为,但是这种情况又如何呢?

c++ arrays string indexing undefined-behavior

9
推荐指数
1
解决办法
417
查看次数

为什么 C++ 允许使用负索引和大于数组长度减 1 的索引进行索引?

为什么编译器不阻止我使用大于数组长度减一的整数和负整数对数组进行索引?

允许这样做的理由是什么?

c++ arrays indexing

0
推荐指数
1
解决办法
2618
查看次数

标签 统计

arrays ×2

c++ ×2

indexing ×2

string ×1

undefined-behavior ×1