Problem with std::allocator::deallocate()

I'm trying to implement a stack data structure, but I having some problems with std::allocator::deallocate(). Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <memory>
#include <iostream>

using namespace std;

template<class T, int N> class Stack
{
public:
	Stack() : elem(alloc.allocate(N)), top(N-1) { }

	void pop_back()
	{
		alloc.deallocate(&elem[top], 1);
		// I found the problem, I should have used alloc.destory()!
		--top;
	}
	void push_back(const T& val)
	{
		
	}

	int available() const { return N; }
	int size() const { return top; }

	T& operator[](int index)
	{
		if(index > top || index < 0) throw exception("out of range");
		return elem[index];
	}
	const T& operator[](int index) const
	{
		if(index > top || index < 0) throw exception("out of range");
		return elem[index];
	}
	const T& back() { return elem[N-1]; }
private:
	T* elem;
	int top;
	allocator<T> alloc;
};

int main()
{
	Stack<int, 10> obj;
	for(int i = 0; i < obj.size(); ++i)
	{
		obj[i] = i + 1;
	}
	obj.pop_back(); // doesn't want to deallocate

	cout << "Please enter a character to exit:" << '\n';
	char ch;
	cin >> ch;
	return 0;
}


Thanks in advance!
Last edited on
> Here's the problem
¿what is the problem?
@ne555

Sorry for not including everything, I was in a rush. Check the code I edited it.
I find the problem. I should have used alloc.destory()!
Topic archived. No new replies allowed.