473,408 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

STL data structures using memory

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!
Jul 19 '05 #1
2 6547
> 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.
Jul 19 '05 #2
"wtnt" <wt**@konzoo.com> wrote in message
news:4f**************************@posting.google.c 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/
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
6
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. ...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
15
by: CMOS | last post by:
one of the projects im working in currently requires use of ultra large sized maps, lists, vector, etc. (basically stl containers). Sizes might grow up to 1000 Million entries. since it is...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
7
by: pereges | last post by:
I've to store an array of structures: typedef struct { double origin; double e_field_at_origin_real, e_field_at_origin_imag; double direction; double pathlength; int depth; }ray;
2
by: =?Utf-8?B?dmxhZGltaXI=?= | last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll functions do: 1) allocate global memory for internal structures 2) controls dll subsystem (communicate by sockets,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.