STL data structures using memory

Discussion in 'C++' started by wtnt, Nov 11, 2003.

  1. wtnt

    wtnt Guest

    Hello,
    I've been using the STL libraries for some time, but still don't know
    the ins and outs of its implementation. Could this be because there's
    more than 1 implementation?
    Does anyone know of a good book out there or article that details how
    one should handle memory usage when using the STL data structures,
    like vector, map, etc.
    Specifically, I mean things like when you put a key (char*) into a
    map, does it copy this? Can I delete it right away? Can I only delete
    it only after I've finished using my map? What about objects and not
    just primitive types.
    Same with vector, if I have a vector of myStruct. Do I need to go
    through the vector and delete them or does the vector destructor do
    that?
    In general, I've been going with the rule of if they are pointers, I
    am responsible, otherwise the vector class will handle it. But what
    if I do:

    vector<myObject> myVector;
    myObject* obj = new myObject();
    myVector.push_back(*obj);

    Then what happens? Was this copied? Should I put delete obj here, or
    at the end of the program, or never?

    You see what I mean. A book that really explains the memory handling
    would be very useful. thanks!
     
    wtnt, Nov 11, 2003
    #1
    1. Advertising

  2. wtnt

    John Brown Guest

    > vector<myObject> myVector;
    > myObject* obj = new myObject();
    > myVector.push_back(*obj);
    >
    > Then what happens? Was this copied? Should I put delete obj here, or
    > at the end of the program, or never?
    >
    > You see what I mean. A book that really explains the memory handling
    > would be very useful. thanks!


    Simply put, all collection classes store *copies* of whatever you store.
    Each stored object must therefore be "copy constructible", i.e., your
    user-defined types (classes and structs) must provide both a copy
    constructor and copy assignment operator that each produce an *exact* copy
    of the original (for all intents and purposes). You can rely on the
    compiler-generated versions of these however (implicitly created if you
    don't create your own), provided that they also produce an exact copy -
    otherwise you must provide your own (the compiler-generated versions do a
    member-wise copy of all members only which may not suffice if memory or
    other resources must be internally copied - simply copying member pointers
    to allocated memory for instance won't do since the memory itself doesn't
    get copied, only the pointers themselves - so you need to write your own
    copy constructor and copy assignment operator to duplicate this memory). The
    bottom line is that you must free any resources that you yourself allocate.
    Thus, you must delete "obj" in your example above because the call to
    "push_back" merely stores a copy of this as discussed (by invoking its copy
    constructor or copy assignment operator). The original must still be deleted
    by you.
     
    John Brown, Nov 11, 2003
    #2
    1. Advertising

  3. wtnt

    Cy Edmunds Guest

    "wtnt" <> wrote in message
    news:...
    > Hello,
    > I've been using the STL libraries for some time, but still don't know
    > the ins and outs of its implementation. Could this be because there's
    > more than 1 implementation?
    > Does anyone know of a good book out there or article that details how
    > one should handle memory usage when using the STL data structures,
    > like vector, map, etc.
    > Specifically, I mean things like when you put a key (char*) into a
    > map, does it copy this? Can I delete it right away? Can I only delete
    > it only after I've finished using my map? What about objects and not
    > just primitive types.
    > Same with vector, if I have a vector of myStruct. Do I need to go
    > through the vector and delete them or does the vector destructor do
    > that?
    > In general, I've been going with the rule of if they are pointers, I
    > am responsible, otherwise the vector class will handle it. But what
    > if I do:
    >
    > vector<myObject> myVector;
    > myObject* obj = new myObject();
    > myVector.push_back(*obj);


    This is a poor way to use std::vector. Much better:

    vector<myObject> myVector;
    myVector.push_back(myObject());

    I wouldn't use operator new() unless the design were polymorphic. It's
    slower and prone to memory management problems. If you must, do this:

    typedef boost::shared_ptr<myObject> ObjectPtr;
    vector<ObjectPtr> myVector;
    myVector.push_back(ObjectPtr(new myObject()));

    Unless myObject is a base class for a family of polymorphic classes this
    approach doesn't make much sense.

    >
    > Then what happens? Was this copied? Should I put delete obj here, or
    > at the end of the program, or never?
    >
    > You see what I mean. A book that really explains the memory handling
    > would be very useful. thanks!


    Standard containers don't do anything special with pointers. AFAIK they all
    make shallow copies of whatever you put in them. I never use raw pointers in
    standard library containers because I want the containers to manage the
    memory for me.

    The best book I know on the standard library is Josuttis ISBN 0-201-37926-0

    --
    Cy
    http://home.rochester.rr.com/cyhome/
     
    Cy Edmunds, Nov 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kushal
    Replies:
    2
    Views:
    6,277
    tom_usenet
    Jan 30, 2004
  2. tweak
    Replies:
    14
    Views:
    2,820
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  4. utab

    Data structures or STL

    utab, Mar 31, 2006, in forum: C++
    Replies:
    5
    Views:
    415
    Noah Roberts
    Mar 31, 2006
  5. sdv
    Replies:
    2
    Views:
    110
Loading...

Share This Page