Purpose of allocator

hi all,
i try to make a vector-like array container, just for experiment :), i am not a experienced one indeed. When i search about a little bit, i found myself in deepless rabbit hole. my question is could you describe briefly what the allocator is. From this point i've tried to make a dynamiccaly growing container using allocator (of course my design is completely depends on what i understand about allocators) and using c-style malloc given below. Two of them using block of memory for size groving. Are these approaches right or completely wrong? can you briefly describe whether the answer is yes or no.

Sorry for the english,(i wish u understand what i meant above) and thanks in advance.

Allocator approach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	void push_back(const int& a) {
		if(map > size) inflate(sizeof(int)*(size+size));
		allocint.construct(&data[map++], int(a));
	}

void list::inflate(size_t sz) {
		int* newdata = allocint.allocate(sz, data);
		memcpy(newdata, data, sizeof(int)*size);
		//copy(data, &data[map], newdata);
		allocint.deallocate(data, sizeof(int)*size);
		data = newdata;
		size = sz / sizeof(int);
		printf(" Size = %d \n",size);
	}


Malloc Approach
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		void* operator new(size_t) {
			if(map > size-1) inflate(sizeof(stash)*(size+size));
			return &mem_pool[map++]; //+ (map++ * sizeof(stash));
		}

void push_back(int a) {
	new stash(a);
}

void list::inflate(size_t sz) {
		mem_pool = (stash*) realloc(mem_pool, sz);
		size = sz / sizeof(stash);
		//printf("Inflated %d \n",size);
}


Do not use realloc/memcpy in C++ it will make your vector to fail miserably when copying non-TriviallyCopyable data. And I have feeling that it would fail with any non-POD type.

Allocators is higher level C++ abstraction over low-level allocation functions. Default allocator does little more than calls malloc. Custom allocators can provide with alternative memory allocation strategies.
http://stackoverflow.com/questions/4642671/c-memory-allocators
http://en.wikipedia.org/wiki/Allocator_%28C%2B%2B%29
http://stackoverflow.com/questions/826569/compelling-examples-of-custom-c-stl-allocators
ty for the response and the links i'll begin to study them...
Topic archived. No new replies allowed.