473,434 Members | 1,829 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,434 software developers and data experts.

vectors stl with pointers

hi all,
I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));

when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
thx
Sam
Jul 19 '05 #1
3 7954
> when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?

you will have to delete it on your own!

Jul 19 '05 #2
"berthelot samuel" <sa**************@voila.fr> wrote:
I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));

when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?


No, you will have to do it yourself. The pop methods just remove the
pointer from the vector. The std::vector class cannot know if it has to
delete the object that is being referenced by the pointer; depending on
the situation it may or may not be the appropriate thing to do. Rather
than using raw pointers, you may also opt for putting boost::shared_ptr
of the boost library (http://www.boost.org/) in the vector. This way you
don't have to worry about ownership issues.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #3

"berthelot samuel" <sa**************@voila.fr> wrote in message
news:ca************************@posting.google.com ...
hi all,
I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));

when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
thx
Sam


No you have to do it one your own, also you have to keep track of copies of
your vector.

This is why you shouldn't use raw pointers in an STL class (or anywhere
similar). Do yourself a favour and learn about smart pointers, will save you
months of debugging tricky pointer problems. Have a look at the smart_ptr
class from www.boost.org for instance, or get a good book.

john
Jul 19 '05 #4

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

Similar topics

2
by: J. Campbell | last post by:
I have a class that contains an array of integers that hold the state of my 'system'. The system must be updated periodically. I need to update the whole system at once, at which point the system...
12
by: Fred Ma | last post by:
Hello, I was looking at Meyers's "Effective STL", item 23 about choosing between vectors and maps (at least that the choice for me). In many cases, using sorted vectors is faster for lookups. ...
5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
4
by: mp | last post by:
I am doing pairwise comparisons between 2 vectors of chars and permuting one vector and storing the resulting calculations in a vector<float> then I find a p-value among other stats. I have to do...
5
by: roberts.noah | last post by:
It is my understanding that if you contain vectors or maps you don't need to create copy constructors because the default calls that constructor. It is my understanding that if you use these types...
1
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I...
5
by: Jim | last post by:
Hi, Just wondering which is better vector<record *r; r.push_back(new record(x,y)); or vector<recordr; r.push_back(record(x,y));
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.