自定义分配器问题 [英] Custom Allocators question

查看:61
本文介绍了自定义分配器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我不确定我是在处理C ++问题还是编译问题,

所以请原谅我,如果我在错误的地方问。如果是这样,也许

有人可以指引我到更合适的地方。


从gcc 3.2.3迁移到gcc 3.4.3会出现以下情况< br $> b $ b up。


我有以下内容:

// typedef std :: vector< TestClass *> TestClassVector;

typedef std :: vector< TestClass *,TestAllocator< TestClass> >

TestClassVector;


TestClassVector a;

TestClassVector * tmpVecPtr =& a;

TestClass * tmpPtr = new TestClass(5);

a.push_back(tmpPtr);


if((* tmpVecPtr)[0]!= NULL )

cout<<"" hello main"<< endl;


使用自定义分配器时,此换行符:

(* tmpVecPtr)[0]!= NULL)


认为这个字段是TestClass并且反对TestClass *

它应该是是。如果我使用第一个typedef(没有指定分配器

指定),它可以使用gcc 3.4.3进行编译。另外两个编译都很好

下gcc 3.2.3


我不清楚编译器是否有更严格的合规性

我需要在allocator类中添加一个方法来进行编译,

或者究竟我的问题是什么。


如果有人可以脱掉对此我会非常感激。


非常感谢!

我会附上以下完整代码:

#include< iostream>

#include< vector>


使用命名空间std;


#ifndef MEMORY_H

#include< memory>

#define MEMORY_H

#endif

模板< class T>

class TestAllocator;


template<>

class TestAllocator< void>

{

public:

typedef void * pointer;

typedef const void * const_pointer;


typedef void value_type;


模板< class U>

struct rebind

{

typedef TestAllocator< U>其他;

};

};


模板< class T>

类TestAllocator

{

public:

//类型定义

typedef T value_type;

typedef size_t size_type;

typedef ptrdiff_t difference_type;

typedef T *指针;

typedef const T * const_pointer;


typedef T&参考;

typedef const T& const_reference;


指针地址(参考值)const {return& value; }


const_pointer地址(const_reference value)const {return& value;

}


//构造函数和析构函数

TestAllocator()throw(){}


TestAllocator(const TestAllocator& copy){}


模板< class U>

TestAllocator(const TestAllocator< U>&)throw(){}


~TestAllocator()throw(){ }


//分配运营商

TestAllocator& operator =(const TestAllocator& rhs){return * this;

}


内联指针

allocate(size_type n,const_pointer) = 0)

{

指针p;

size_type size;


size = n * sizeof(T);


p =(指针)::操作员新(大小);


返回p;

}

inline void

deallocate(指针p,size_type n = 1)

{

:: operator delete(p);

}


void

construct(指针p,const T& val)

{

new((void *)p)T(val);

}


void

destroy(指针p)

{

((T *)p) - > ~T();

}


size_type

max_size()const throw()

{

return std: :numeric_limits< std :: size_t> :: max()/ sizeof(T);

}


模板< class U>

struct rebind

{

typedef TestAllo CATOR< U>其他;

};

};


类TestClass

{

public:


TestClass(int a):b(a){};

~TestClass();

int getNum(){return b;};


int b;

};


// typedef std :: vector< TestClass *> TestClassVector;

typedef std :: vector< TestClass *,TestAllocator< TestClass> >

TestClassVector;

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

{

TestClassVector a;

TestClassVector * tmpVecPtr =& a;

TestClass * tmpPtr = new TestClass(5);

a.push_back(tmpPtr) ;


if((* tmpVecPtr)[0]!= NULL)

cout<<" hello main"<< endl;

}

Hi All,

I''m not sure if I''m dealing with a C++ question or a compiler question,
so please forgive me if I''m asking in the wrong spot. If so, maybe
someone can direct me to more appropriate spot.

In migrating from gcc 3.2.3 to gcc 3.4.3 the following situation comes
up.

I have the following:
//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;

TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);

if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;

When using the custom allocator this line breaks:
(*tmpVecPtr)[0] != NULL )

thinking that this field is a TestClass and opposed to the TestClass*
which it should be. If I use the first typedef(with no allocator
specified) it compiles fine with gcc 3.4.3. Also both compile fine
under gcc 3.2.3

I''m not clear if there has been a tightening compliance of the compiler
and i need to add a method to the allocator class to make this compile,
or what exactly my problem is.

If anyone can shed some light on this I would be very appreciative.

Thanks much!
I''ll attach the complete code below:

#include <iostream>
#include <vector>

using namespace std;

#ifndef MEMORY_H
#include <memory>
#define MEMORY_H
#endif
template <class T>
class TestAllocator;

template <>
class TestAllocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;

typedef void value_type;

template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};

template <class T>
class TestAllocator
{
public:
// Type definitions
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;

typedef T& reference;
typedef const T& const_reference;

pointer address(reference value) const { return &value; }

const_pointer address(const_reference value) const { return &value;
}

// Constructors and destructor
TestAllocator() throw() {}

TestAllocator(const TestAllocator& copy) {}

template <class U>
TestAllocator(const TestAllocator<U>&) throw() {}

~TestAllocator() throw() {}

// Assignment Operator
TestAllocator& operator=(const TestAllocator& rhs) { return *this;
}

inline pointer
allocate(size_type n, const_pointer = 0)
{
pointer p;
size_type size;

size = n * sizeof(T);

p = (pointer) ::operator new(size);

return p;
}
inline void
deallocate(pointer p, size_type n = 1)
{
::operator delete(p);
}

void
construct(pointer p, const T& val)
{
new((void*) p) T(val);
}

void
destroy(pointer p)
{
((T*) p)->~T();
}

size_type
max_size() const throw()
{
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}

template <class U>
struct rebind
{
typedef TestAllocator<U> other;
};
};

class TestClass
{
public:

TestClass(int a): b(a){};
~TestClass();
int getNum() { return b;};

int b;
};

//typedef std::vector<TestClass* > TestClassVector;
typedef std::vector<TestClass*,TestAllocator<TestClass> >
TestClassVector;
int main(int argc, char *argv[])
{
TestClassVector a;
TestClassVector * tmpVecPtr = &a;
TestClass * tmpPtr = new TestClass(5);
a.push_back(tmpPtr);

if ( (*tmpVecPtr)[0] != NULL )
cout<<"hello main"<<endl;
}

推荐答案

na ********** @ yahoo.com 写道:
使用自定义分配器时,此换行符:
( * tmpVecPtr)[0]!= NULL)
When using the custom allocator this line breaks:
(*tmpVecPtr)[0] != NULL )




错误信息是什么?



What is the error message?


啊......细节!... :)


在矢量定义之间切换w /或没有

指定的分配器导致它与之不匹配!= NULL。在这种情况下

指定了分配器。


谢谢..


test.cpp:在功能中`int main(int,char **)'':

test.cpp:128:错误:''运算符!=''不匹配

'' tmpVecPtr-> std :: vector< _Tp,_Alloc> :: operator [] [with _Tp =

TestClass *,_ Alloc = TestAllocator< TestClass>](0ul)!= 0l''

/usr/lib/gcc/x86_64-redhat-linux/3.4.3 /../../../../ include / c ++ / 3.4.3 / bits / vector.tcc:

在成员函数中`void std :: vector< _Tp,

_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc: :指针,std :: vector< _Tp,_Alloc>>,const _Tp&)[with _Tp =

TestClass *,_ Alloc = TestAllocator< TestClass>]'':

Ahh.. details!... :)

Switching between the vector definition either w/ or without the
Allocator specified causes it to not match the != NULL. in the case
where an Allocator is specified.

Thanks..

test.cpp: In function `int main(int, char**)'':
test.cpp:128: error: no match for ''operator!='' in
''tmpVecPtr->std::vector<_Tp, _Alloc>::operator[] [with _Tp =
TestClass*, _Alloc = TestAllocator<TestClass>](0ul) != 0l''
/usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:
In member function `void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
TestClass*, _Alloc = TestAllocator<TestClass>]'':


natespama ... @ yahoo.com写道:
natespama...@yahoo.com wrote:
在矢量定义之间切换w /或没有指定
分配器导致它不 匹配!= NULL。在
中指定了分配器。

test.cpp:在函数`int main(int,char **)'':
test.cpp:128:错误:''运算符!=''在'tmpVecPtr-> std :: vector< _Tp,_Alloc> :: operator [] [与_Tp =
TestClass *,_ Alloc = TestAllocator< TestClass>](0ul)!= 0l''


!= 0l''???那是什么?


/usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++ /3.4.3/bits/vector.tcc:


x86_64? 64位?

在成员函数`void std :: vector< _Tp,
_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename
_Alloc :: pointer,std: :vector< _Tp,_Alloc>>,const _Tp&)[with _Tp =
TestClass *,_ Alloc = TestAllocator< TestClass>]'':
TestClass * tmpPtr = new TestClass(5);


非常值得怀疑。根据C ++标准,''new''抛出异常(并且不会返回

NULL)以防OOM(内存不足)(不是

实际上例如在Linux中)。无论如何...

if((* tmpVecPtr)[0]!= NULL)
Switching between the vector definition either w/ or without the
Allocator specified causes it to not match the != NULL. in the case
where an Allocator is specified.

test.cpp: In function `int main(int, char**)'':
test.cpp:128: error: no match for ''operator!='' in
''tmpVecPtr->std::vector<_Tp, _Alloc>::operator[] [with _Tp =
TestClass*, _Alloc = TestAllocator<TestClass>](0ul) != 0l''
!= 0l'' ??? What''s that?

/usr/lib/gcc/x86_64-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/vector.tcc:

x86_64? 64Bit?
In member function `void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
TestClass*, _Alloc = TestAllocator<TestClass>]'': TestClass * tmpPtr = new TestClass(5);
Highly questionable. ''new'' throws an exception (and doesn''t return
NULL) in case of OOM (out-of-memory) according to the C++ Standard (not
actually e.g. in Linux). Anyway ...
if ((*tmpVecPtr)[0] != NULL )




尝试:

if((* tmpVecPtr)[0]!=(TestClass *)0)


重新考虑你的设计!



Try:
if ((*tmpVecPtr)[0] != (TestClass*) 0)

Re-think your design!


这篇关于自定义分配器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆