Boost logo

Boost :

From: radelber_at_[hidden]
Date: 2000-12-13 11:40:58

Forgive me if this is the wrong place. I have a question concerning
implementation rather than semantics.

I have a need for a simple database that stores one copy of an
object. If an object being inserted already exists, a pointer to it
is returned. If the use_count drops to zero the object should be

I'm thinking of using a set of shared_ptr. Is this a good solution?

I have a class, myEvent
typedef shared_ptr < myEvent > EventPtr;
typedef EventPtr::iterator EventIter;

set<EventPtr> EventDb; // my database

MyEvent* pQuery = new MyEvent(...) // create new entry
EventPtr cp(pQuery) // create shared_ptr
                                    // insert the object
pair<EventIter, bool> pair1 = EventDb.insert(cp);
cp = *pair1.first;

1. Is this correct? Reguardless of pair1.second, the original value
of cp (pQuery) is destroyed and cp points to either the newly
inserted (pQuery), or a previous shared_ptr.

1b. Or if I reassign cp, does the inserted pQuery end up being

2. How do I know when to delete the cp from the set? the shared_ptr
will delete the object it points to, but do I have to check if
use_count < 1?

3. Does the constant creation of new objects and deletions at insert
time cause memory fragmentation problems?

Thanks in advance,


Rob Adelberg radelber_at_[hidden]
Arinc, Inc.

Boost list run by bdawes at, gregod at, cpdaniel at, john at