【发布时间】:2011-09-16 10:29:22
【问题描述】:
我已经尝试实现 lengthof (T* v) 函数很长一段时间了,到目前为止没有任何成功。
对于 T v[n] 数组,有两种基本的、众所周知的解决方案,一旦数组衰减为 T* v 指针,这两种解决方案都是无用的,甚至是危险的。
#define SIZE(v) (sizeof(v) / sizeof(v[0]))
template <class T, size_t n>
size_t lengthof (T (&) [n])
{
return n;
}
有一些涉及包装类和容器的解决方法,如 STLSoft 的 array_proxy、boost::array、std::vector 等。它们都有缺点,缺乏数组的简单性、语法糖和广泛使用。
当 delete [] 需要知道数组的长度时,编译器通常会使用涉及编译器特定调用的解决方案存在神话。根据 C++ FAQ Lite 16.14,编译器使用两种技术来了解要释放多少内存:过度分配和关联数组。在过度分配时,它会多分配一个字大小,并将数组的长度放在第一个对象之前。另一种方法显然将长度存储在关联数组中。是否可以知道 G++ 使用哪种方法,并提取适当的数组长度?开销和填充物呢?对非编译器特定的代码有任何希望吗?甚至是非平台特定的 G++ 内置函数?
还有涉及重载operator new []和operator delete []的解决方案,我实现了:
std::map<void*, size_t> arrayLengthMap;
inline void* operator new [] (size_t n)
throw (std::bad_alloc)
{
void* ptr = GC_malloc(n);
arrayLengthMap[ptr] = n;
return ptr;
}
inline void operator delete [] (void* ptr)
throw ()
{
arrayLengthMap.erase(ptr);
GC_free(ptr);
}
template <class T>
inline size_t lengthof (T* ptr)
{
std::map<void*, size_t>::const_iterator it = arrayLengthMap.find(ptr);
if( it == arrayLengthMap.end() ){
throw std::bad_alloc();
}
return it->second / sizeof(T);
}
在我得到一个奇怪的错误之前它工作得很好:lengthof 找不到数组。事实证明,G++ 在这个特定数组的开头分配了比它应该有的多 8 个字节。尽管 operator new [] 应该返回整个数组的开头,将其称为 ptr,但调用代码却得到了 ptr+8,因此 lengthof(ptr+8) 显然失败并出现异常(即使没有,它也可能有可能返回了错误的数组大小)。那 8 个字节是某种开销还是填充?不能是前面提到的过度分配,该函数对许多数组都正常工作。它是什么以及如何禁用或解决它,假设可以使用 G++ 特定的调用或诡计?
编辑: 由于可以通过多种方式分配 C 样式的数组,因此通常不可能通过指针来判断任意数组的长度,正如 Oli Charlesworth 所建议的那样。但是基于 Ben Voigt 的想法,非衰减静态数组(参见上面的模板函数)和使用自定义运算符 new [] (size_t, size_t) 分配的数组是可能的:
#include <gc/gc.h>
#include <gc/gc_cpp.h>
#include <iostream>
#include <map>
typedef std::map<void*, std::pair<size_t, size_t> > ArrayLengthMap;
ArrayLengthMap arrayLengthMap;
inline void* operator new [] (size_t size, size_t count)
throw (std::bad_alloc)
{
void* ptr = GC_malloc(size);
arrayLengthMap[ptr] = std::pair<size_t, size_t>(size, count);
return ptr;
}
inline void operator delete [] (void* ptr)
throw ()
{
ArrayLengthMap::const_iterator it = arrayLengthMap.upper_bound(ptr);
it--;
if( it->first <= ptr and ptr < it->first + it->second.first ){
arrayLengthMap.erase(it->first);
}
GC_free(ptr);
}
inline size_t lengthof (void* ptr)
{
ArrayLengthMap::const_iterator it = arrayLengthMap.upper_bound(ptr);
it--;
if( it->first <= ptr and ptr < it->first + it->second.first ){
return it->second.second;
}
throw std::bad_alloc();
}
int main (int argc, char* argv[])
{
int* v = new (112) int[112];
std::cout << lengthof(v) << std::endl;
}
不幸的是,由于编译器的任意开销和填充,到目前为止还没有可靠的方法来确定自定义运算符 new [] (size_t) 中动态数组的长度,除非我们假设填充小于数组元素之一的大小。
但是,正如 Ben Voigt 所建议的那样,还有其他类型的数组也可以计算长度,因此构造一个可以接受多种数组(及其长度)的包装类应该是可能和可取的。它的构造函数,并且可以隐式或显式转换为其他包装类和数组类型。不同类型数组的不同生命周期可能是个问题,但可以通过垃圾回收来解决。
【问题讨论】:
-
您认为这比将代码转换为基于 std::vector 或 boost::array 更简单?过度分配可能有很多原因。您可以使用不同的分配器进行原型设计,例如 boost::pool_allocator
-
是否有可能在不使用哨兵值的情况下在 C 本身中获取 C 样式数组的长度? C 中的字符串以 null 结尾的事实让我相信这通常是不可能的。
-
请注意,您的技巧仅适用于动态分配的数组。静态和自动数组通常不会在任何地方存储任何元信息。
-
@JAB:在 C 中这是不可能的,数组会立即衰减为指针。
-
@JAB, @Matthieu:我很确定
sizeof方法在 C 中有效。数组在sizeof上下文中不会衰减。