1

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.

igntec
  • 1,006
  • 10
  • 24
  • 6
    Placement `new` does *not* allocate memory! It is actually highly misleading because it is almost completely unrelated to conventional `new` – by default, it just calls an object’s constructor. – Konrad Rudolph Nov 09 '12 at 14:42
  • 2
    There is so much wrong with this... are you just trying to make a linked list?! – Kerrek SB Nov 09 '12 at 14:43
  • 6
    To add what what Konrad said, after `delete p;`, `p` no longer points to usable storage, and thus you cannot construct a new `int` object at the place it points to. This (mis)use of placement new makes the whole thing have undefined behaviour. – R. Martinho Fernandes Nov 09 '12 at 14:44

1 Answers1

1
p = new(p) int(2);    // (p) - memory will be allocated at address
                      // on which p points

This is where you're wrong for the first time. new(p) T does not allocate any memory. Rather than that, it just creates a T at address p, assuming that p points to some memory where a T can be constructed. Also, since there is no memory allocated, you cannot just pass such pointers to delete, as this will attempt to release the not allocated storage. Rather than that, you need to manually invoke the destructor and dispose of the memory depending on however you acquired it.

Placement new is a rather advanced concept that you very likely do not need at this time. In fact, even "simple" manual memory/object lifetime management is something you probably should not be bothering yourself with at this time of your learning. What book are you learning C++ from? Have you seen this book list?

FYI: I haven't looked at your code below that line.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445