newbie: vectors

P

phiroc

Hi,

lets say that you create a vector and add an object to it, thus

v03.push_back(new Zone("1", 0, 2, line.substr(0,2), "xxx", "o"));
....

It is a good idea to delete the Zone objects added to the vector,
towards the end of the program, in order to release the memory, thus

for (itr = v03.begin(); itr != v03.end(); itr++) {
cout << "Nom: " << (*itr)->getNom().c_str() << endl;
....

delete *itr;

}

or does the vector's destructor automatically delete the Zones?

Many thanks.

phiroc
 
S

shazled

lets say that you create a vector and add an object to it, thus

v03.push_back(new Zone("1", 0, 2, line.substr(0,2), "xxx", "o"));
...

It is a good idea to delete the Zone objects added to the vector,
towards the end of the program, in order to release the memory, thus

for (itr = v03.begin(); itr != v03.end(); itr++) {
cout << "Nom: " << (*itr)->getNom().c_str() << endl;
...

delete *itr;

}

or does the vector's destructor automatically delete the Zones?

You need to delete the Zone objects yourself, as you suggested. If
Zone objects aren't that big you may find it safer to just have a
vector<Zone> rather than vector<Zone *>. This means no new or delete
operations.

If you really need pointers, another possibility is to use something
like std::tr1::shared_ptr<Zone> (or boost::shared_ptr) rather than a
normal pointer. This will delete the Zone objects automatically when
they are no longer in the vector and no longer used by the rest of the
program.

Saul
 
P

phiroc

You need to delete the Zone objects yourself, as you suggested. If
Zone objects aren't that big you may find it safer to just have a
vector<Zone> rather than vector<Zone *>. This means no new or delete
operations.

If you really need pointers, another possibility is to use something
like std::tr1::shared_ptr<Zone> (or boost::shared_ptr) rather than a
normal pointer. This will delete the Zone objects automatically when
they are no longer in the vector and no longer used by the rest of the
program.

Saul

Thanks Saul.

Is is legitimate to delete the Zone (ie, delete *itr) as I loop
through the vector to display its contents, or is that bad form?

Furthermore, how would you use a shared_ptr here:

v03.push_back(new Zone("1", 0, 2, line.substr(0,2), "xxx", "o"));

phiroc
 
K

Kermit Mei

Thanks Saul.

Is is legitimate to delete the Zone (ie, delete *itr) as I loop
through the vector to display its contents, or is that bad form?
Furthermore, how would you use a shared_ptr here:

v03.push_back(new Zone("1", 0, 2, line.substr(0,2), "xxx", "o"));

phiroc

I think it's really a good idea.It looks like the sub-object model in
Qt platform.
But you'd better to encapsulate this process in some methods which
provides some suitable
interfaces.Or it looks unsafe.

kermit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top