Just a quick check - calling the destructor

I wanted to make sure I am doing everything here correctly - this is a test of a situation where calling the destructor is needed.
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
class A
{
	int* p;
public:
	A() : p(new int) { cout << "Constructing" << endl; }

	int& Modify()
	{
		return(*p);
	}

	~A()
	{
		delete p;
		cout << "Destructing" << endl;
	}
};

int main()
{
	cout << "Making room for A: ";
	A* a = (A*)(new __int8[sizeof(A)]);
	new (a) A;

	a->Modify() = 7;
	cout << a->Modify() << endl;

	cout << "Calling A's Destructor: ";
	a->~A();
	delete[] (__int8*)(a);

	cin.sync();
	cin.ignore();
}
I think so http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10

1
2
3
4
5
allocator<A> alloc;
A *ptr = alloc.allocate(1); //one element
alloc.construct( ptr, A() ); //uses placement new
alloc.destroy( ptr ); 
alloc.deallocate( ptr, 1 );
Thanks - should I be worried about alignment problems?
Topic archived. No new replies allowed.