Error placing new for array in MSVC 2012?

when i do the following:

T* ptr2 = new (ptr1) T();

      

I am getting ptr1 == ptr2

When I do:

T* ptr2 = new (ptr1) T[6];

      

I am getting in MSVC 2012 in debug, ptr2> ptr1 (3 bytes off)

However, the code in the "new" file:

inline void *__CRTDECL operator new[](size_t, void *_Where) _THROW0()
    {   // construct array with placement at _Where
    return (_Where);
    }

      

I am assuming some debugging information has been added, but I cannot figure it out.

The initial ptr1 is obtained via malloc, so I think it should be bound to any possible type, so I don't think alignment is an issue.

Am I doing something that shouldn't work here? Why ptr2! = Ptr1? Thank!

+3


source to share


1 answer


I don't think C ++ placement of new is strictly required to place the start of an object that can be seen by client code at the beginning of the buffer you passed to the new one.

You only need to place it somewhere inside. It's perfectly cool for the compiler to do whatever it wants with the space, provided it doesn't have any buffers.



This way, you don't have to depend on ptr1 and ptr2 having the same value for any use of the new's placement, not just that.

I'm not sure about this. I could be wrong.

0


source







All Articles