Skip to main content
gamedev.net
🔒 Locked

Memory management and smart pointers

Started by JimPrice Oct 12, 2004 at 6:26 PM 0 replies 600+ views
Original Post
JimPrice
JimPrice
Hi, I've been doing a fair bit of reading about memory management and smart pointers, and would like to ask a few questions of the esteemed audience (smarm, smarm). From what I've read, memory management looks to handle a few issues, such as: fragmentation and coalescing statistic generation - mean, max amount of memory usage allocation auditing - tracking memory blocks and making sure they are freed correctly Smart pointers often seem to be integrated into this structure, in that they handle managing their own associated memory (primarily a way of getting rid of worrying about when to delete them). For example, the Boost::shared_ptr explicitly says in its description : "Every occurence of the new keyword in the code should have the form shared_ptr p(new Y) ... If you follow this guideline, it naturally follows that you will have no explicit deletes..." Most memory manager implementations I have seen rely on over-riding global new/delete (or object-specific new/delete). How easy then is it to implement a custom memory manager when you hand new/delete responsibilities over to a libray like boost (I believe there is a similar issue with some of the IO functions that handle their own memory acquisitions). So - the question boils down to this. How are people integrating these concepts in their own designs? Is there a way of using a library such as Boost with a self-written memory manager? If you want this functionality do you need to write the smart pointers yourself, so as to integrate them with your custom memory manager? Or is there an alternative way to meet these objectives? Thanks in advance for any help, Jim.
Fruny
Fruny
Quote:
Original post by JimPrice
Most memory manager implementations I have seen rely on over-riding global new/delete (or object-specific new/delete).


It's generally a bad idea to do so, since it does prevent other part of the code to, well, overload it for themselves. If you do have two library that try and overload the global new/delete operators,, you're in trouble. Object-specific versions are OK.

Quote:
How easy then is it to implement a custom memory manager when you hand new/delete responsibilities over to a libray like boost


boost::shared_ptr is not aware of any tricks you might have pulled with new and delete. It'll use whatever operator delete is associated with the type of the object it holds - or whatever destruction function you passed it. Most of Boost is implemented as C++ include files, and thus get compiled into your program using whatever altered new/delete you have.

Quote:
Is there a way of using a library such as Boost with a self-written memory manager?


I think it should work straight out of the box.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.