Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL data structures using memory

Reply
Thread Tools

STL data structures using memory

 
 
wtnt
Guest
Posts: n/a
 
      11-11-2003
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!
 
Reply With Quote
 
 
 
 
John Brown
Guest
Posts: n/a
 
      11-11-2003
> 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.


 
Reply With Quote
 
 
 
 
Cy Edmunds
Guest
Posts: n/a
 
      11-11-2003
"wtnt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data structures or STL utab C++ 5 03-31-2006 03:46 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
STL Data Structures, Sorted Insertion? Kushal C++ 2 01-30-2004 04:37 PM
memory and storing references to data structures sdv Ruby 2 09-26-2003 06:04 AM



Advertisments