/*Orion Lawlor's Simple C++ Examples, olawlor@acm.org
Shows how to use the buildin "placement new" operator
to call a C++ class's constructor on an existing chunk
of memory (allocated some other way, like malloc).
*/
#include <stdio.h>
#include <stdlib.h>
#include <new>   /* includes placement new operator */

class foo {
public:
	double a;
	int x;
	foo() {printf("foo constructor called (this=%p)\n",this);}
};

int main() {
	void *ptr=malloc(sizeof(foo));
	printf("Allocator data is at %p\n",ptr);
	foo *f=new (ptr) foo; /* just call foo's constructor at ptr */
	printf("Foo allocated at %p\n",f);
}
/*<@>
<@> ******** Program output: ********
<@> Allocator data is at 0x804a008
<@> foo constructor called (this=0x804a008)
<@> Foo allocated at 0x804a008
<@> */
