My project is in c++11, using vs2012.
Right now I don’t feel the need of using custom memory management but, what arrangements should I take in order to facilitate an eventual future modification?
I thought of using a macro for “new”/”new[]”/”delete”/”delete[]” and typedefs for containers and smart pointers.
What are the best practises?
From my point of view, all you have to do is basically decide on a certain convention that you will use throughout your implementation. A good template for making your architecture allocator aware is to look on how this is achieved for the STL containers and try to design you data structures like them. If you look for example on the
std::vectorinterface the second parameter to this container is always the Allocator type. The Allocator has to follow a certain interface that will allow easy replacing of the default allocator implementation by a custom one.But coming back to code that you write: In a project that I work on we defined it like follows. For an object that is supposed to consume large amounts of memory we define a templated super class that allows to specify a custom allocator.
Now, if you define a subclass of this you would:
Thus the new object
MyClasswill use the specified allocator of the parent class. If you now implement you own allocator you can simply replace either the default template parameter in the super class or you can specify it explicitly in your sub-class.Using this approach you are more or less safe in using a custom allocator at a later point in time.