I have been set a test piece of code that should write the answer to the test to screen and I am struggling to understand the role of the memory manager class within it. I have debugged the code and it appears to be not entering the if statement at...
if (newItem)
it appears this needs to evaluate to true to continue correctly.
stringBuffer is a char array containing ItemType_Int
Is line 11 really ever executed? To debug, print the value of newItem after line 11 and resultPtr at the end of AlloateMemory. Also, are these malloc and free the ones from cstdlib ? What is in "malloc.h" and "MemoryManager.h" ?
Thanks for the reply, the else if in line 9 is entered and line 11 is executed but it doesnt seem to affect newItem, which confuses me!
Yes this is malloc from cstdlib, I have included MemoryManager.h below, I am finding it difficult to understand the role of this class and how to implement it so it can allocate memory with its overloaded operator new function so any words of wisdom on this would help.
The pointer is passed by value. It's a copy of the pointer that will be returned by operator new. Assigning to a copy does not affect the original. Make this void*& or void** or make it return the pointer, etc.
Still having a few problems, i appear to have fixed the MemoryManager class and the program is looping through the main while loop correctly except for when it comes to processing an ItemType_String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
elseif (_stricmp(stringBuffer, "ItemType_String") == 0)
{
newItem = new(memoryManager) ItemType_String();
}
if (newItem)
{
itemArray[numItems] = newItem;
numItems ++;
if (newItem->LoadItemFromFile(memoryManager, ptr))
{
/// Move onto the next element.
stringLength = 0;
fread(&stringLength, sizeof(int), 1, ptr);
}
After line 3 newItem holds an itemType_String but line 11 is not executing properly,
In locals the values read:
m_memoryManagerAllocatedFrom=0x0000000 m_value=0x00000000 <bad Ptr>