473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of pointers.

A quick question:

vector <T *test_vec;
T testT;
test_vec.push_back(&testT);

T * testT2p = new T;
test_vec.push_back(testT2p);

Now at the end do I need to explicitly delete the vector (test_vec).
How should I make sure that the memory allocation is ok?

Thanks,

May 1 '07 #1
3 1776
Dave wrote:
A quick question:

vector <T *test_vec;
T testT;
test_vec.push_back(&testT);

T * testT2p = new T;
test_vec.push_back(testT2p);

Now at the end do I need to explicitly delete the vector (test_vec).
Only the elements that were explicitly 'new'ed.
How should I make sure that the memory allocation is ok?
There is no other non-implementation-specific way except to keep
some kind of flag to distinguish between the ones you got from 'new'
and the others.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '07 #2
On May 1, 1:05 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Dave wrote:
A quick question:
vector <T *test_vec;
T testT;
test_vec.push_back(&testT);
T * testT2p = new T;
test_vec.push_back(testT2p);
Now at the end do I need to explicitly delete the vector (test_vec).

Only the elements that were explicitly 'new'ed.
How should I make sure that the memory allocation is ok?

There is no other non-implementation-specific way except to keep
some kind of flag to distinguish between the ones you got from 'new'
and the others.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
To make things strait - you don't have to 'delete' your
test_vec since it's a stack variable. You do have to
worry about proper memory deallocation though
every time you have a collection of pointers.

One simple solution is to use std::vector<boost::shared_ptr<T.
The Boost shared pointer allows you to specify the destruction
function (that defaults to normal delete if not specified)
Wrap your stack/global object pointers with empty function,
and leave heap-allocated objects be 'delete'd.
That's almost the same as keeping the flag as above,
but moves your effort to vector insert time (when you know
where the object came from) instead of vector remove time
(when you don't.)
--
Nikolai

May 1 '07 #3
Dave wrote:
A quick question:
vector <T *test_vec;
T testT;
test_vec.push_back(&testT);
T * testT2p = new T;
test_vec.push_back(testT2p);
Now at the end do I need to explicitly delete the vector (test_vec).
If it is a local variable, no, but you'll have to delete
testT2p. Formally, only after the destructor of the vector has
been called.
How should I make sure that the memory allocation is ok?
Use the Boehm collector.

Otherwise, something like:

vector< T* test_vec ;
T testT ;
test_vec.push_back( &testT ) ;
std::auto_ptr< T testT2p( new T ) ;
test_vec.push_back( testT2p.get() ) ;

will handle this particular case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 2 '07 #4

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

Similar topics

9
2949
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
3503
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
13
2123
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
11
2714
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
2295
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
3601
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
5
5457
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
3233
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
3975
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
3490
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
7002
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
7203
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
6885
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
7379
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...
1
4908
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
4588
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
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
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...

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.