473,569 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_b ack(*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 6560
> vector<myObject > myVector;
myObject* obj = new myObject();
myVector.push_b ack(*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.co m> wrote in message
news:4f******** *************** ***@posting.goo gle.com...
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_b ack(*obj);
This is a poor way to use std::vector. Much better:

vector<myObject > myVector;
myVector.push_b ack(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_p tr<myObject> ObjectPtr;
vector<ObjectPt r> myVector;
myVector.push_b ack(ObjectPtr(n ew 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
3844
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 that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own...
5
4778
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. "payload") using pointers. Examples of these are binary trees, hash tables etc. Thus, the message itself contains only a pointer to the actual data....
6
2614
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. My basic need is:
11
3748
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 appreciate it: 1. What exactly is a Python list? If one writes a, then is the complexity Theta(n)? If this is O(1), then why was the name "list" chosen? If...
44
5748
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
2576
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 impossible to have all this data in memory, im planning to implement these containers to hold data both in memory and disk at the same time. im not sure...
19
6317
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 can all access. It seems like a needless waste of memory to make them all maintain their own copy of the same data in RAM at the same time. ...
7
1918
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
1905
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, read a files, etc). These control functions triggers change of a values in the subsystem internal structures. 3) dealocate memory for internal...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.