i am trying to allocate pointers of the first order one by one at memory. Pointer of the second order must walk through this queue and handle to objects, created on that pointers. Placement new works fine when i allocate the queue of objects(not pointers to them), but when i am trying the same with pointers the trouble happens.
int* p;
p = new int(1);
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
First object is created and deleted to initialize pointer with some address, next objects will be created in cycle
p = new(p) int(2); // (p) - memory will be allocated at address
// on which p points
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
p++;
p = new(p) int(3); // (p) - memory will be allocated at address
// on which p points
printf("%p - %d\n", p, *p); // address of int and its value
delete p;
Output:
01C1FFD0 - 1
01C1FFD0 - 2
01C1FFD4 - 3;
int** pp;
pp = new int*;
printf("%p\n", pp); // address of int*
*pp = new int(1);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
pp = new(pp) int*;
printf("%p\n", pp); // address of int*
*pp = new int(2);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
pp++;
pp = new(pp) int*;
printf("%p\n", pp); // address of int*
*pp = new int(3);
printf("%p - %d\n", *pp, **pp); // address of int and its value
delete *pp;
delete pp;
If int is not created by *pp then if pp is incremented it is not real to delete pp later, if int is created by *pp then there is something strange with addresses and second value.
Output:
01C1FFD0
01C1FFE0 - 1
01C1FFD0
01C1FFD0 - 29491152
01C1FFD4
01C1FFD0 - 3
I would be very grateful for advice how to organize this queue correct.