Hi, I'm overriding the new operator to manually allocate heap space when using the new keyword.
Usually using a stack-allocated heap container with pointers to its items-
CArray<CObject*> objects;
-is fine. but I'm picky and I want to make buffers-
CArray<CObject> objects;
-that let me modify POD types. So, I allocate them on the stack which is naturally suited for their use:
CVertex vertex;
objects.push_back(vertex);
But it corrupts the heap (I've had one of those bad weeks) and gives a crazy error:
0xC0000017: Not Enough Quota.
I pre-allocate the array objects and then use the = operator internally in push_back().
I solved the problem by allocating the temporary objects on the heap and then adding them to the array. But it doesn't seem right, and I just don't get it.
As requested, some code:
CArray::push_back( T& newElement )
{
m_internalElements[allocatedSize] = newElement;
allocatedSize++;
}
CArray::preallocate_and_initialize( size_t itemCount )
{
T* newInternalElements = mem::allocate_and_initialize( T, itemCount );
//copy over
}
Note that it all works with heap allocated CVertex's as I am using these objects in a binary search of the buffer elements (finding the right item in the index buffer for a given vertex) and it builds a mesh perfectly fine!