Do I need to use destructor here?

P

Pushkar Pradhan

I have a function which holds a list of my data structure PARTICLE,
initially the list is declared empty, but I grow it in this function.
Now my question is do I have to write a destructor (since the datatype
is my own) to delete the list?
Or does it get deleted automatically once the function exits?
Code:

compute_hull(....)
{
list<PARTICLE> hull2;

for(i = 0; i < X; i++) {
/* depending on some condition */
hull2.push_back(...);
}

/* is destructor reqd. for hull2 now? */
}

Thanks,
Pushkar Pradhan
 
G

Gianni Mariani

Pushkar said:
I have a function which holds a list of my data structure PARTICLE,
initially the list is declared empty, but I grow it in this function.
Now my question is do I have to write a destructor (since the datatype
is my own) to delete the list?
Or does it get deleted automatically once the function exits?
Code:

compute_hull(....)
{
list<PARTICLE> hull2;

for(i = 0; i < X; i++) {
/* depending on some condition */
hull2.push_back(...);
}

/* is destructor reqd. for hull2 now? */
}

hull2 is a variable. list<> has a destructor that will delete all the
"PARTICLE" objects. I have no idea if PARTICLE requires a destructor.

G
 
P

Pushkar Pradhan

Gianni said:
hull2 is a variable. list<> has a destructor that will delete all the
"PARTICLE" objects. I have no idea if PARTICLE requires a destructor.

G
I tried doing a "~hull2()", but the compiler gives syntax error, does
that help?
 
G

Gianni Mariani

Pushkar said:
I tried doing a "~hull2()", but the compiler gives syntax error, does
that help?

The destructor call would be: hull2.~list<PARTICLE> but this would be
*really bad* to call because it's automagically called for you when you
return (or take an exception and the stack is rolled back past a
compute_hull stack frame).

Rule - don't go calling destructors unless you're doing your own memory
management (i.e. writing your own container). Use the language
semantics to do this for you.
 

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

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top