이 질문은 std :: map에 삽입하는 동안 사용자 지정 할당 자 인스턴스의 생성에 관한 것입니다. 여기 STL 용 사용자 지정 메모리 할당 자
그것을 사용하는 작은 프로그램과 함께std::map<int,int>
위한 사용자 할당이다 : 여기
#include <stddef.h>
#include <stdio.h>
#include <map>
#include <typeinfo>
class MyPool {
public:
void * GetNext() {
return malloc(24);
}
void Free(void *ptr) {
free(ptr);
}
};
template<typename T>
class MyPoolAlloc {
public:
static MyPool *pMyPool;
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;
typedef T value_type;
template<typename X>
struct rebind
{ typedef MyPoolAlloc<X> other; };
MyPoolAlloc() throw() {
printf("-------Alloc--CONSTRUCTOR--------%08x %32s\n", this, typeid(T).name());
}
MyPoolAlloc(const MyPoolAlloc&) throw() {
printf(" Copy Constructor ---------------%08x %32s\n", this, typeid(T).name());
}
template<typename X>
MyPoolAlloc(const MyPoolAlloc<X>&) throw() {
printf(" Construct T Alloc from X Alloc--%08x %32s %32s\n", this, typeid(T).name(), typeid(X).name());
}
~MyPoolAlloc() throw() {
printf(" Destructor ---------------------%08x %32s\n", this, typeid(T).name());
};
pointer address(reference __x) const { return &__x; }
const_pointer address(const_reference __x) const { return &__x; }
pointer allocate(size_type __n, const void * hint = 0) {
if (__n != 1)
perror("MyPoolAlloc::allocate: __n is not 1.\n");
if (NULL == pMyPool) {
pMyPool = new MyPool();
printf("======>Creating a new pool object.\n");
}
return reinterpret_cast<T*>(pMyPool->GetNext());
}
//__p is not permitted to be a null pointer
void deallocate(pointer __p, size_type __n) {
pMyPool->Free(reinterpret_cast<void *>(__p));
}
size_type max_size() const throw() {
return size_t(-1)/sizeof(T);
}
void construct(pointer __p, const T& __val) {
printf("+++++++ %08x %s.\n", __p, typeid(T).name());
::new(__p) T(__val);
}
void destroy(pointer __p) {
printf("-+-+-+- %08x.\n", __p);
__p->~T();
}
};
template<typename T>
inline bool operator==(const MyPoolAlloc<T>&, const MyPoolAlloc<T>&) {
return true;
}
template<typename T>
inline bool operator!=(const MyPoolAlloc<T>&, const MyPoolAlloc<T>&) {
return false;
}
template<typename T>
MyPool* MyPoolAlloc<T>::pMyPool = NULL;
int main(int argc, char *argv[]) {
std::map<int, int, std::less<int>, MyPoolAlloc<std::pair<const int,int> > > m;
//random insertions in the map
m.insert(std::pair<int,int>(1,2));
m[5] = 7;
m[8] = 11;
printf("======>End of map insertions.\n");
return 0;
}
은이 프로그램의 출력 : 출력 쇼
-------Alloc--CONSTRUCTOR--------bffcdaa6 St4pairIKiiE Construct T Alloc from X Alloc--bffcda77 St13_Rb_tree_nodeISt4pairIKiiEE St4pairIKiiE Copy Constructor ---------------bffcdad8 St13_Rb_tree_nodeISt4pairIKiiEE Destructor ---------------------bffcda77 St13_Rb_tree_nodeISt4pairIKiiEE Destructor ---------------------bffcdaa6 St4pairIKiiE ======>Creating a new pool object. Construct T Alloc from X Alloc--bffcd9df St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE +++++++ 0985d028 St4pairIKiiE. Destructor ---------------------bffcd9df St4pairIKiiE Construct T Alloc from X Alloc--bffcd95f St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE +++++++ 0985d048 St4pairIKiiE. Destructor ---------------------bffcd95f St4pairIKiiE Construct T Alloc from X Alloc--bffcd95f St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE +++++++ 0985d068 St4pairIKiiE. Destructor ---------------------bffcd95f St4pairIKiiE ======>End of map insertions. Construct T Alloc from X Alloc--bffcda23 St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE -+-+-+- 0985d068. Destructor ---------------------bffcda23 St4pairIKiiE Construct T Alloc from X Alloc--bffcda43 St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE -+-+-+- 0985d048. Destructor ---------------------bffcda43 St4pairIKiiE Construct T Alloc from X Alloc--bffcda43 St4pairIKiiE St13_Rb_tree_nodeISt4pairIKiiEE -+-+-+- 0985d028. Destructor ---------------------bffcda43 St4pairIKiiE Destructor ---------------------bffcdad8 St13_Rb_tree_nodeISt4pairIKiiEE
마지막 두 열은 그 std::pair<const int, int>
에 대한 할당자는 맵에 삽입 될 때마다 생성됩니다. 왜 이것이 필요한가? 이것을 막을 수있는 방법이 있습니까?
감사합니다.
편집 :이 코드는 g ++ 버전 4.1.2에서 x86 컴퓨터에서 테스트되었습니다. 64 비트 시스템에서 실행하려면 최소한 return malloc(24)
행을 변경해야합니다. return malloc(48)
으로 변경하면 효과가 있습니다.
위 출력과 같이 매번 구성하는 것은 불법이 아닙니다. STL/C++이 이런 식으로해야한다는 고유 한 이유가 있습니까? 그렇지 않다면 구현을 어떻게 억제하여 그것을 어떻게 변경합니까? –
C++ 03에서는 할당자가 상태 비 저장 (비어 있음)하는 것으로 가정 할 수 있습니다. 그렇게하면 사본을 만드는 것이 거의 무료입니다. 귀하의 경우에는 복사 생성자와 할당 연산자가 내부 포인터를 복사하고 매번 새로운 풀을 생성하지 않을 수도 있습니다. –
@Bo - 질문의 제목은 복사 비용 생성자가 아닙니다! 대신 St13_Rb_tree_nodeISt4pairIKiiEE에 대한 할당 자로부터 St4pairIKiiE에 대한 할당자를 생성하는 것은 생성자입니다. –