Re: Custom allocator trouble
On 27 May, 00:00, Lance Diduck <lancedid...@nyc.rr.com> wrote:
[snip]
stateful allocators is perhaps the most cumbersome of all the
allocators models. You will spend most of your time figuring out how
to make sure every object that needs to use a particular memory region
actually does.
Not necessarily but I agree that unrestrained use of stateful
allocators can be practically impossible to debug. This is why I would
confine their use to just one module in the system and have objects
copied into and out of that module such that outside the module the
default allocator is used.
I have worked on system that attempted to use this method of placing
object in shared memory: it was a disaster.
I am curious, Lance. Would this be the system that we both know of
that made a really big thing about how stateful allocators are broken
in the language? I do not recall it being a disaster. Sure there are
pitfalls but when I used the facility it was fine provided we kept the
scope very strict. This did require some education for the junior
programmers.
What you really want to do is make SimpleAllocator have a default
constructor, ie. make it a Singleton, or, have nothing but static
methods. Initialize the statics to your shared memory region before
first use. THIS is the simplest allocator. Then make class specific
new/delete point to it just like you see in the Meyers books. And to
boot, cut and paste your STL implementation std::allocator , rename
it, and change whre it gets its memory from. This is a 10 minute job
at most.
Whilst this might work fine in simple cases I think the issue is where
an object that needs to like in shared memory has private data members
that would normally be allocated on the heap, such as std::string
members. I know you do not have this problem because you tend to avoid
using std::string in favour of a string class that gives more precise
control over buffer allocation. But many people tempted to use custom
allocators might not have such a string class. They are not easy to
write, especially if they are to cope with both wide and narow
characters.
Regards,
Andrew Marlow
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]