gpt4 book ai didi

c++ - 自定义分配器有时会因 STL vector 而崩溃

转载 作者:太空狗 更新时间:2023-10-29 23:33:35 24 4
gpt4 key购买 nike

我得到了一个自定义分配器,它实现了它自己的分配策略、它自己的 malloc/free 等。现在,我被要求将这个自定义分配器与 STL 容器(可以是 vector 或其他一些).我创建了一个类,比如说 my_stdAllocator,它是一个符合 ISO C++ 标准 的接口(interface)。通过这个类,我调用了分配器的方法。例如:

template <class T>
class my_stdAllocator {

// ...other required stuff...

// allocate 'num' elements of type T
pointer allocate(size_type num, const_pointer = 0) {
return static_cast<T*>(MYAllocator::instance()->malloc(num));
}

// deallocate memory at 'p'
void deallocate(pointer p, size_type num=0) {
MYAllocator::instance()->free(p);
}

// initialize allocated memory at 'p' with value 'value'
void construct(pointer p, const T& value) {
::new ((void*)p) T(value);
}


// destroy elements of initialized memory at p
void destroy(pointer p) {
p->~T();
}

// ...other required stuff...

} // end my_stdAllocator

自定义分配器通常像一个魅力一样工作:它已经过广泛测试,它确实提高了性能,限制了碎片等。当我将它用作 STL 容器的分配器时,(比如,一个 vector )它有这个奇怪的行为的原因在于它有时可以正常工作,而有时会因段错误而崩溃。

举个例子,它使用 char 的 vector 正确分配和释放:

typedef char TP;

int main(int argc, char* argv[]) {

std::vector<TP, my_stdAllocator<TP> > vec;

std::string s ("Whatever string, no matter how long...");

std::string::iterator it;
for (it=s.begin(); it<s.end(); ++it)
vec.push_back(*it);

...

在 vector 中“手动”输入数字是可以的

typedef double TP;

int main(int argc, char* argv[]) {

std::vector<TP, my_stdAllocator<TP> > vec;

// "manual" push_back
vec.push_back(3.2);
vec.push_back(6.4);
vec.push_back(9.6);
vec.push_back(12.8);
vec.push_back(15.1);
vec.push_back(18.3);
vec.push_back(21.5);

...

当通过循环插入元素时,它因段错误而停止:

typedef int TP;

int main(int argc, char* argv[]) {

std::vector<TP, ff_stdAllocatorInst<TP> > vec;

for(unsigned int i=0; i<size; ++i)
vec.push_back( (TP) i );

...

当为至少一定数量的元素保留空间时,它就像一个魅力:

typedef int TP;

int main(int argc, char* argv[]) {

std::vector<TP, ff_stdAllocatorInst<TP> > vec;

vec.reserve(size);
for(unsigned int i=0; i<size+150; ++i)
vec.push_back( (TP) i );

...

请注意,当使用像这样的 placement new 时不会发生上述段错误:

void *p = MYAllocator::instance()->malloc(size);
std::vector<TP> *vec = new (p) std::vector<TP>
for(unsigned int i=0; i<size; ++i)
vec->push_back( (TP) i );

...

正如我已经说过的,自定义分配器已经过测试并且工作正常。我的类是 C++ 标准和自定义分配器之间的简单接口(interface)。我已经尝试使用 gdb 对其进行调试,但它没有帮助:底层分配器没问题,我的代码中肯定有一些错误,但我不明白哪里出了问题!

最佳答案

在调用自定义分配器的 malloc 函数时,您需要乘以要分配的对象的大小:

pointer allocate(size_type num, const_pointer = 0) {
return static_cast<T*>(MYAllocator::instance()->malloc(num*sizeof(T)));
}

关于c++ - 自定义分配器有时会因 STL vector 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com