![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|