I write my own memory system in C ++ (for performance reasons, additional debugging information, and therefore I can allocate memory with alignment of 16 bytes), and I encounter a problem with a new [].
It seems that calling new [] results in an additional 4 bytes being allocated indicating the number of elements in the array, which resets the alignment of all subsequent objects. So my question is this: is there a way to disable the use of these 4 extra bytes with the compiler flag, pragma declaration, etc.?
Here is an example:
Matrix* transforms = new( matrixHeap, ALIGN_16, __FILE__, __LINE__ ) Matrix[31];
transforms[0] = Matrix::Identity;
When searching in the Visual Studio 2013 debugger, I see these values:
returned by new 0x0F468840
transforms 0x0F468844
Finally, I look into the raw memory, and I see this:
0x0F468840 1F 00 00 00
0x0F468844 01 00 00 00
****** 40 transforms[0], . , 31. , 4 . , transforms[0] , []?
new []:
void* operator new[] ( size_t size, Heap* heap, uint32_t alignment, char* file, int lineNumber )
{
size_t alignedSize = size + alignment - 1;
void* unalignedPtr = heap->Allocate( alignedSize );
void* alignedPtr = (void*) (((size_t) unalignedPtr + alignment - 1) & ~(alignment - 1));
return alignedPtr;
}