Re: Deleteing automatic variables

Discussion in 'C++' started by mintiSPAMBLOCK@yahoo.com, Jul 10, 2004.

  1. Guest

    Blue Ocean wrote:
    > Suppose I have a class. It has some members and some variables and
    > all that. The methods are not important in this question.
    >
    > class IntSet
    > {
    > public:
    > ...
    > private:
    > int threshold;
    > int[] set;
    > }
    >
    > Suppose I have a destructor and I want to make sure that all of the
    > resources previously owned by this class will be given back to the
    > system when an instance of the class is deleted. Should the
    > destructor be:
    >
    > IntSet::~IntSet()
    > {
    > delete[] set;
    > delete threshold;
    > }
    >
    > Or should it be:
    >
    > IntSet::~IntSet()
    > {
    > delete[] set;
    > }
    >
    > Or are both variants wrong?
    >
    > Thanks in advance for the help.


    As others have pointed out that you can't achieve deletion on
    automatic variables, however if you know your variables have a limited
    scope you can simple put it in a proper scope/block as in

    void foo(void)
    {
    .....
    {

    int x;
    // ONLY this scope/block uses x
    .....
    }
    // x is no longer needed.
    ....
    }

    --
    Imanpreet Singh Arora
    isingh AT acm DOT org
    , Jul 10, 2004
    #1
    1. Advertising

  2. wrote:

    > As others have pointed out that you can't achieve deletion on
    > automatic variables, however if you know your variables have a limited
    > scope you can simple put it in a proper scope/block as in


    Perhaps a bit of picking at nits:
    Automatic variables are deleted when the program execution
    exits the scope that the variables are defined int. This,
    to me, is "achieves deletion". One cannot force the
    deletion of an automatic variable before the execution
    leaves the scope (although one _can_ force the leaving
    of scope).


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jul 12, 2004
    #2
    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. Emilio

    Problem deleteing files

    Emilio, Oct 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    352
    Rajesh.V
    Oct 23, 2003
  2. Blue Ocean

    Deleteing automatic variables

    Blue Ocean, Jul 10, 2004, in forum: C++
    Replies:
    3
    Views:
    366
    Howard
    Jul 12, 2004
  3. Replies:
    5
    Views:
    489
    John Carson
    Mar 14, 2005
  4. msnnews.msn.com
    Replies:
    10
    Views:
    535
    gerry
    Aug 22, 2006
  5. timmy
    Replies:
    3
    Views:
    242
    Mikael Olofsson
    Nov 14, 2006
Loading...

Share This Page