Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 9th, 2006, 2:57 PM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Memory manager

Hi!

I am woking on a memory manager that will keep info about memory allocation (of course). The problem is that it gives me an incorrect size of the allocation, and I can't see where the error is. It must be something with the MemoryManager class that messes it up. When I run the program I print adress, size, file, function and line to the console window. But size is 48 when it should be 4, and I cant see why. As you can see in the code, the only thing I do is to allocate a simple integer of 4 bytes. What am I doing wrong? Why is the output so big?

Thanks alot for your help!

/Klarre

#include <iostream>
#include <vector>
#include <string>

struct TrackInfo
{
	char*        adress;
	size_t       size;
	size_t       line;
	std::string  file;
	std::string  function;
};

class MemoryManager
{
	friend void *operator new(size_t size);

public:
	static MemoryManager* singleton();

	void *operator new(size_t size);
	void operator delete(void *ptr);

	void addTrackingInfo(size_t line, std::string file, std::string function);
	void removeTrackingInfo(void* a, const char* c, unsigned int d);

	void printToConsole();

private:
	MemoryManager() {}

	static MemoryManager* mSingleton;

	char*  _mAdress;
	size_t _mSize;

	std::vector<TrackInfo> mTrackInfos;
};

MemoryManager* MemoryManager::mSingleton = NULL;

// -----------------------------------------------------------------------------
MemoryManager* MemoryManager::singleton()
{
	if(mSingleton == NULL)
		mSingleton = new MemoryManager();

	return mSingleton;
}

// -----------------------------------------------------------------------------
void* MemoryManager::operator new(size_t size)
{
	void *ptr = malloc(size);

	return reinterpret_cast<char*>(ptr);
}

// -----------------------------------------------------------------------------
void MemoryManager::operator delete(void *ptr)
{
	free(ptr);
}

// -----------------------------------------------------------------------------
void MemoryManager::addTrackingInfo(size_t line, std::string file, std::string function)
{
	TrackInfo info;

	info.line     = line;
	info.file     = file;
	info.function = function;
	info.adress   = _mAdress;
	info.size     = _mSize;

	mTrackInfos.push_back(info);
}

// -----------------------------------------------------------------------------
void MemoryManager::removeTrackingInfo(void* a, const char* c, unsigned int d)
{
	// NOT IMPLEMENTED YET
}

// -----------------------------------------------------------------------------
void MemoryManager::printToConsole()
{
	for(unsigned int i = 0; i < mTrackInfos.size(); ++i)
		std::cout << "Adress  : " << &mTrackInfos[i].adress  << std::endl
		          << "Size    : " << mTrackInfos[i].size     << std::endl
				  << "File    : " << mTrackInfos[i].file     << std::endl
				  << "Function: " << mTrackInfos[i].function << std::endl
				  << "Line    : " << mTrackInfos[i].line     << std::endl;
}

// -----------------------------------------------------------------------------
void *operator new(size_t size)
{
	void *ptr = malloc(size); // WHY IS size = 48 ???

	MemoryManager::singleton()->_mAdress = reinterpret_cast<char*>(ptr);
	MemoryManager::singleton()->_mSize   = size;

	return reinterpret_cast<char*>(ptr);
}

// -----------------------------------------------------------------------------
void operator delete(void *ptr)
{
	free(ptr);
}

#define new (MemoryManager::singleton()->addTrackingInfo(__LINE__, __FILE__, __FUNCTION__), false) ? NULL : new

int main()
{
	MemoryManager::singleton()->printToConsole(); // Nothing to print...

	int* x = new int;

	delete x;

	MemoryManager::singleton()->printToConsole();

	std::cin.get();

	return 0;
}
Klarre is offline   Reply With Quote
Old Mar 9th, 2006, 4:44 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
The value of 48 is probably equivalent to sizeof(MemoryManager).

Incidentally #define'ing any C or C++ keyword (new in your case) is specified by the standard as yielding undefined behaviour.

And, unrelated to your problem, __FUNCTION__ is (IIRC, but may stand corrected) a non-standard extension.
grumpy is offline   Reply With Quote
Old Mar 10th, 2006, 4:36 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by grumpy
The value of 48 is probably equivalent to sizeof(MemoryManager).

Incidentally #define'ing any C or C++ keyword (new in your case) is specified by the standard as yielding undefined behaviour.

And, unrelated to your problem, __FUNCTION__ is (IIRC, but may stand corrected) a non-standard extension.
You are right __FUNCTION__ is not ansi or ISO C++, but the Microsoft compiler and GCC use it, the latter explain that __FUNCTION__ equates to __func__ which is a C99 macro.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 10th, 2006, 7:08 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
ANSI and ISO C++ are the same thing.

When the C++ standard was being developed, it was referred to as (something like) "the draft ANSI C++ standard" as the committee working on it worked under auspices of ANSI (American National Standards Institute). In late 1998, the standard was finally ratified by ANSI, and it formally became an ANSI standard. A short time later (no, I don't know how long) the ANSI standard was adopted "essentially as is" by ISO (International Standards Organisation) [the only changes were minor formatting and minor changes of titles, not of content], so the C++ standard became an ISO standard.

Some people consider that being an ISO standard is more important than being an ANSI standard (eg it's truly International rather than American) but as the ANSI C++ standard and the ISO C++ standard are the same thing, it's a moot point in this case.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC