Click here to Skip to main content
13,254,077 members (60,999 online)
Click here to Skip to main content
Articles » Languages » C / C++ Language » STL » Downloads

Stats

229.2K views
1.7K downloads
100 bookmarked
Posted 18 Aug 2003

C++ Standard Allocator, An Introduction and Implementation

, 18 Aug 2003
Introduction to the allocator concept, as well as implementing a policy-driven allocator template class
#include "allocator.hpp"
#include "typetraits.hpp"
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <deque>
#include <stack>

int main() {
	std::stack<int, Dot::Allocator<int> > s;
	std::deque<int, Dot::Allocator<int> > d;
	std::map<int, int, Dot::Allocator<int> > m;

	std::vector<int, Dot::Allocator<int, Dot::TrackAllocPolicy<int> > > v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);
	v.push_back(7);
	v.push_back(8);
	v.push_back(9);
	v.push_back(10);
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().CurrentAllocations()) << std::endl;
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().TotalAllocations()) << std::endl;
	std::cout << static_cast<Dot::Traits::StreamTypeTraits<std::size_t>::StreamT>(v.get_allocator().PeakAllocations()) << std::endl;

	std::vector<int, Dot::Allocator<int, Dot::FixedArrayAllocPolicy<int, 10> > > v2;
	v2.reserve(10);
	v2.push_back(1);
	v2.push_back(2);
	v2.push_back(3);
	v2.push_back(4);
	v2.push_back(5);
	v2.push_back(6);
	v2.push_back(7);
	v2.push_back(8);
	v2.push_back(9);
	v2.push_back(10);

	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > > vTest;

	std::cout << "Pre" << std::endl;
	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > >::iterator p = vTest.begin();
	std::list<int, Dot::Allocator<int, Dot::SmallObjectAllocPolicy<int, 2> > >::iterator pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	vTest.push_back(5);
	vTest.push_back(10);
	vTest.push_back(20);

	std::cout << "During" << std::endl;
	p = vTest.begin();
	pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	vTest.pop_front();
	vTest.pop_front();
	vTest.pop_front();

	std::cout << "After" << std::endl;
	p = vTest.begin();
	pEnd = vTest.end();
	while ( p != pEnd ) {
		std::cout << (*p) << ", ";
		++p;
	}
	std::cout << std::endl;

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lai Shiaw San Kent
Web Developer
Singapore Singapore
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 19 Aug 2003
Article Copyright 2003 by Lai Shiaw San Kent
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid