Results 1 to 4 of 4
Hi all! I'm relatively new to C++, so the following question could be simple to some...
I'm writing a class wich should manage memory allocations. It's not a garbage collector, ...
- 06-17-2007 #1Linux User
- Join Date
- Aug 2005
- Location
- Italy
- Posts
- 401
std::malloc and memory management
Hi all! I'm relatively new to C++, so the following question could be simple to some...
I'm writing a class wich should manage memory allocations. It's not a garbage collector, but simple a template class wich should wrap all memory functions: this is the first step. This class has 3 functions: malloc, realloc and free.
As you can see, it's very simple. But, in implementation I call the <b>real</b> malloc:Code:template <typename T> class duAlloc { public: static T *malloc(duint n); static T *realloc(T *mem, duint n); static void free(T *mem); }; #include <util/duAlloc.cpp>
The code execution result recursive infinitely, because malloc in interpreted as duAlloc::malloc...Code:template <class T> T *duAlloc<T>::malloc(duint n) { T *m; duAssert(n > 0); if ((m = malloc(sizeof(T) * n)) == NULL) { abort(); } else return (m); }
I know I could write std::malloc for specify the malloc routine, but compiler say that malloc is not defined in the std namespace... anyone can help me?
...then. I'd like to make memory reference countable: so having a generic pointer user can query memory size and a reference count. A duAlloc::free call will free memory only if the reference count is reset. How I can start implementing this?
Many thank to all!When using Windows, have you ever told "Ehi... do your business?"
Linux user #396597 (http://counter.li.org)
- 06-17-2007 #2Linux User
- Join Date
- Aug 2005
- Location
- Italy
- Posts
- 401
Resolved... problems includes cstdlib inside a mine namespace...

However... the question about reference/size countable memory is always open...
When using Windows, have you ever told "Ehi... do your business?"
Linux user #396597 (http://counter.li.org)
- 06-17-2007 #3
I've never implemented something like this. However, if you want to do this with anything (and not just your own special classes where a reference count could be built in), why not just use a dictionary to map pointers to reference count? You could write a retain function that would increment a reference count, and then just free it at the end of the function, and the free'ing would only happen if that was the last reference.
As for querying: you would already have the dictionary for reference count, you could use a second one to map pointers to sizes. Or make a struct called reference_info or something that stores both pieces of information.DISTRO=Arch
Registered Linux User #388732
- 06-18-2007 #4Linux User
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
What you're looking for are smart pointers.
Take a look at following links:
Smart pointer - Wikipedia, the free encyclopedia
Smart Pointers - What, Why, Which?
Smart Pointers
HTH
The Unforgiven
Registered Linux User #358564


Reply With Quote