In debug heap I can get size of array, which was created by new[]
:
int size = *((int *)((char *)ptr - 16));
It is working correctl
You are relying on an implementation detail. That's how your particular implementation stores the size of the memory region where the array is placed. As you already see, the size of the memory region may be bigger than the size of the allocated array.
If you need to know the size of an array allocated with new[]
, you'll have to keep that value around.