Do I need to use destructor here?

Discussion in 'C++' started by Pushkar Pradhan, Nov 30, 2003.

  1. 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
    Pushkar Pradhan, Nov 30, 2003
    #1
    1. Advertising

  2. Pushkar Pradhan wrote:
    > 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
    Gianni Mariani, Dec 1, 2003
    #2
    1. Advertising

  3. Gianni Mariani wrote:
    > Pushkar Pradhan wrote:
    >
    >> 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
    >

    I tried doing a "~hull2()", but the compiler gives syntax error, does
    that help?
    Pushkar Pradhan, Dec 1, 2003
    #3
  4. Pushkar Pradhan wrote:
    >
    >
    > Gianni Mariani wrote:
    >
    >> Pushkar Pradhan wrote:
    >>
    >>> 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
    >>

    > 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.
    Gianni Mariani, Dec 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joost Kraaijeveld

    Is a private destructor allowed here?

    Joost Kraaijeveld, Feb 4, 2004, in forum: C++
    Replies:
    2
    Views:
    463
    Jonathan Turkanis
    Feb 4, 2004
  2. frs
    Replies:
    20
    Views:
    749
    Alf P. Steinbach
    Sep 21, 2005
  3. arun
    Replies:
    2
    Views:
    540
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    415
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,167
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page