我正在寻找一个gcc支持的C ++语言扩展,以支持非零的数组指针的分配。理想情况下,我可以简单地写:
#include<iostream>
using namespace std;
// Allocate elements array[lo..hi-1], and return the new array.
template<typename Elem>
Elem* Create_Array(int lo, int hi)
{
return new Elem[hi-lo] - lo;
// FIXME what about [expr.add]/4.
// How do we create a pointer outside the array bounds?
}
// Deallocate an array previously allocated via Create_Array.
template<typename Elem>
void Destroy_Array(Elem* array, int lo, int hi)
{
delete[](array + lo);
}
int main()
{
const int LO = 1000000000;
const int HI = LO + 10;
int* array = Create_Array<int>(LO, HI);
for (int i=LO; i<HI; i++)
array[i] = i;
for (int i=LO; i<HI; i++)
cout << array[i] << "\n";
Destroy_Array(array, LO, HI);
}
上面的代码似乎有效,但没有C ++标准定义。具体来说,问题是[expr.add]/4:
当向指针添加或从指针中减去具有整数类型的表达式时,结果具有指针操作数的类型。如果表达式P指向具有n个元素的数组对象x的元素x [i],则表达式P + J和J + P(其中J具有值j)指向(可能是假设的)元素x [i + j]如果0≤i+j≤n;否则,行为未定义。同样,如果0≤i-j≤n,则表达式P-J指向(可能是假设的)元素x [i-j];否则,行为未定义。
换句话说,上面代码中标记为FIXME的行的行为是未定义的,因为它计算的指针在基于0的数组x[0..n]
的x
范围之外。
--std=...
是否有一些gcc
选项告诉它允许直接计算非零数组指针?
如果没有,是否有一种合理的可移植方式来模仿return new Type[hi-lo] - lo;
声明,也许是通过施放到long
并返回? (但我会担心引入更多错误)
此外,这可以通过只需要1个寄存器来跟踪每个数组的方式完成,就像上面的代码一样吗?例如,如果我有array1[i], array2[i], array3[i]
,这只需要3个寄存器用于数组指针array1, array2, array3
,加上一个寄存器用于i
? (类似地,如果冷读取数组引用,我们应该能够直接获取非基于零的指针,而不用仅仅为了在寄存器中建立引用而进行计算)
假设你在linux x86-64上使用gcc,它支持intptr_t
和uintptr_t
类型,它们可以保存任何指针值(有效或无效)并且还支持整数运算。 uintptr_t
更适合这个应用,因为它支持mod 2^64 semantics而intptr_t
有UB病例。
正如评论中所建议的,我们可以使用它来构建一个重载operator[]
并执行范围检查的类:
#include <iostream>
#include <assert.h>
#include <sstream> // for ostringstream
#include <vector> // out_of_range
#include <cstdint> // uintptr_t
using namespace std;
// Safe non-zero-based array. Includes bounds checking.
template<typename Elem>
class Array {
uintptr_t array; // base value for non-zero-based access
int lo; // lowest valid index
int hi; // highest valid index plus 1
public:
Array(int lo, int hi)
: array(), lo(lo), hi(hi)
{
if (lo > hi)
{
ostringstream msg; msg<<"Array(): lo("<<lo<<") > hi("<<hi<< ")";
throw range_error(msg.str());
}
static_assert(sizeof(uintptr_t) == sizeof(void*),
"Array: uintptr_t size does not match ptr size");
static_assert(sizeof(ptrdiff_t) == sizeof(uintptr_t),
"Array: ptrdiff_t size does not match ptr (efficieny issue)");
Elem* alloc = new Elem[hi-lo];
assert(alloc); // this is redundant; alloc throws bad_alloc
array = (uintptr_t)(alloc) - (uintptr_t)(lo * sizeof(Elem));
// Convert offset to unsigned to avoid overflow UB.
}
//////////////////////////////////////////////////////////////////
// UNCHECKED access utilities (these method names start with "_").
uintptr_t _get_array(){return array;}
// Provide direct access to the base pointer (be careful!)
Elem& _at(ptrdiff_t i)
{return *(Elem*)(array + (uintptr_t)(i * sizeof(Elem)));}
// Return reference to element (no bounds checking)
// On GCC 5.4.0 with -O3, this compiles to an 'lea' instruction
Elem* _get_alloc(){return &_at(lo);}
// Return zero-based array that was allocated
~Array() {delete[](_get_alloc());}
//////////////////////////////
// SAFE access utilities
Elem& at(ptrdiff_t i)
{
if (i < lo || i >= hi)
{
ostringstream msg;
msg << "Array.at(): " << i << " is not in range ["
<< lo << ", " << hi << "]";
throw out_of_range(msg.str());
}
return _at(i);
}
int get_lo() const {return lo;}
int get_hi() const {return hi;}
int size() const {return hi - lo;}
Elem& operator[](ptrdiff_t i){return at(i);}
// std::vector is wrong; operator[] is the typical use and should be safe.
// It's good practice to fix mistakes as we go along.
};
// Test
int main()
{
const int LO = 1000000000;
const int HI = LO + 10;
Array<int> array(LO, HI);
for (int i=LO; i<HI; i++)
array[i] = i;
for (int i=LO; i<HI; i++)
cout << array[i] << "\n";
}
请注意,由于intptr_t
,仍然无法将GCC 4.7 Arrays and Pointers计算的无效“指针”强制转换为指针类型:
当从指针转换为整数并再次返回时,结果指针必须引用与原始指针相同的对象,否则行为是未定义的。也就是说,可能不会使用整数运算来避免指针运算的未定义行为,如C99和C11 6.5.6 / 8中所禁止的那样。
这就是为什么array
场必须是intptr_t
类型而不是Elem*
。换句话说,只要在转换回intptr_t
之前调整Elem*
指向原始对象,就会定义行为。