G4.9pool alloc用例

#include <iostream>
#include <vector>
#include <ext\pool_allocator.h>
using namespace std;

template<typename Alloc>
void cookie_test(Alloc alloc, size_t n) {
	typename Alloc::value_type *p1,*p2,*p3;
	p1 = alloc.allocate(n);
	p2 = alloc.allocate(n);
	p3 = alloc.allocate(n);
	
	cout << "p1= " << p1 << '\t' << "p2= " << p2 << '\t' << "p3= " << p3 << endl;
	
	alloc.deallocate(p1, sizeof(typename Alloc::value_type));
	alloc.deallocate(p2, sizeof(typename Alloc::value_type));
	alloc.deallocate(p3, sizeof(typename Alloc::value_type));
}

int main() {

	cout << sizeof(__gnu_cxx::__pool_alloc<int>) << endl;
	vector<int, __gnu_cxx::__pool_alloc<int> > vecPool;
	cookie_test(__gnu_cxx::__pool_alloc<double>(), 1);
	//相距08h(表示不带cookie) 
	cout << endl;
		
	cout << sizeof(std::allocator<int>) << endl;
	vector<int, std::allocator<int> > vec;
	cookie_test(std::allocator<double>(), 1);
	//相距10h(表示带cookie)
	return 0;
}


G4.9的allocator只是以::operator new和::operator delete完成allocate()和deallocate(),没有任何特殊设计

欲使用std::allocatoe以外的allocator,就得自行//include <ext/...>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值