Problems with Memory Manager Class

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (processElement)
			{
				ItemType* newItem = 0;

				if (strcmp(stringBuffer, "ItemType_Char") == 0)
				{
					newItem = new(memoryManager) ItemType_Char();
				}
				else if (_stricmp(stringBuffer, "ItemType_Int") == 0)
				{
					newItem = new(memoryManager) ItemType_Int();
				}
				else if (_stricmp(stringBuffer, "ItemType_String") == 0)
				{
					newItem = new(memoryManager) ItemType_String();
				}

				if (newItem)
				{
					itemArray[numItems] = newItem;
					numItems ++


memoryManager has been declared as class MemoryManager, the cpp file for this is below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "malloc.h"
#include "MemoryManager.h"

/*************************************************************************************************************/

void* 
operator new(size_t size, MemoryManager& mameoryManager)
{
	void* ptr = 0;
	mameoryManager.AllocateMemory(size, ptr);
	return ptr;
}

/*************************************************************************************************************/

void 
operator delete(void* ptr, MemoryManager& mameoryManager)
{
	mameoryManager.FreeMemory(ptr);
}

/*************************************************************************************************************/

MemoryManager::MemoryManager()
{

}

/*************************************************************************************************************/

MemoryManager::~MemoryManager()
{

}

/*************************************************************************************************************/

void	
MemoryManager::AllocateMemory(size_t amountInBytes, void* resultPtr)
{
	resultPtr = malloc(amountInBytes);
}

/*************************************************************************************************************/

void	
MemoryManager::FreeMemory(void* memoryToFree)
{
	free(memoryToFree);
}


Any help would be greatly apprieciated and any further requests for information will be answered swiftly
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef __MEMORYMANAGER_H__
#define __MEMORYMANAGER_H__

class MemoryManager
{
public:
			MemoryManager();

			~MemoryManager();

	void	AllocateMemory(
				size_t amountInBytes, 
				void* resultPtr
				);

	void	FreeMemory(
				void* memoryToFree
				);
protected:
private:
};

extern void* operator new(size_t size, MemoryManager& memoryManager);
extern void operator delete(void* ptr, MemoryManager& memoryManager);

template <class Type>
inline void			
DrDelete(MemoryManager& memoryManager, Type*& ptr)
{
	ptr->~Type();
	memoryManager.FreeMemory(ptr);
	ptr = 0;
};

#endif //#ifndef __MEMORYMANAGER_H__ 
Silly of me not to notice.
1
2
3
4
5
void	
MemoryManager::AllocateMemory(size_t amountInBytes, void* resultPtr)
{
	resultPtr = malloc(amountInBytes);
}
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.
Last edited on
Thanks for your help, i see whats happening a bit more now :)
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
	else if (_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>

Below i have given code for ItemType_String.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "stdio.h"
#include "stdlib.h"
#include "MemoryManager.h"
#include "ItemType.h"
#include "ItemType_String.h"

/******************************************************************************************************************************/

ItemType_String::ItemType_String() : 
	m_value(0),
	m_memoryManagerAllocatedFrom(0)
{

}

/******************************************************************************************************************************/

ItemType_String::~ItemType_String()
{
	if (m_value)
	{
		m_memoryManagerAllocatedFrom->FreeMemory(m_value);
	}
}

/******************************************************************************************************************************/

char*	
ItemType_String::GetSortValue() const
{
	return m_value;
}

/******************************************************************************************************************************/

bool	
ItemType_String::LoadItemFromFile(MemoryManager& manager, FILE* ptr)
{
	m_memoryManagerAllocatedFrom = &manager;
	
	int stringLength = 0;
	size_t amountRead = fread(&stringLength, sizeof(stringLength), 1, ptr);

	if (amountRead == 1)
	{
		void* memoryPtr = 0;
		m_memoryManagerAllocatedFrom->AllocateMemory(stringLength + 1, memoryPtr);
		m_value = static_cast<char*>(memoryPtr);

		size_t amountRead = fread(m_value, stringLength, 1, ptr);
		m_value[stringLength] = '\0';
		
		return amountRead == 1;
	}

	return false;
}


any suggestions are greatly appreciated
Where exactly in LoadItemFromFile does it break ?
Topic archived. No new replies allowed.