Search code examples
c++arrayswdk

Header size of array in C++ for WDK


Not sure if "header" is the correct term, so please correct me if it isn't.

I'm trying to first allocate a memory, and then use an overloaded (placement) new[] operator to initialize an array of class objects (say, MyClass).

Say, the size of a MyClass object is 0x68, and I want an array of 0x20. So the total size is sizeof(MyClass) * 0x20 = 0xD00, or so I thought.

Now when I use my overloaded placement new[] operator :

pArr = new(pAllocatedMem)MyClass[0x20];

The compiler returned size_t to the new[] operator is actually 0xD08. There is an extra 8 bytes. Looking at the value of that 8 bytes, it's used to store the size of the array (0x20 in this case).

So is there a constant definition of what this header size is, say from WDK, that I can use? Does this size changes depends on compilers or what else?


Solution

  • That amount of that extra space, if any, is compiler dependent. For the languange definition [expr.new, paragraph 15]:

    [It] is a non-negative unspecified value representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operator new[].

    It is typically used by the runtime to know how many objects to destroy when delete is eventually called on the array.