deleting pointers in the destructor

This is a discussion on deleting pointers in the destructor within the C++ Programming forums, part of the General Programming Boards category; I know that the destructors of objects will always take care of the simple, "normal" pointers, but I know that ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question deleting pointers in the destructor

    I know that the destructors of objects will always take care of the simple, "normal" pointers, but I know that when pointers within the classes are used to create dynamic arrays and stuff, code must be written inside the destructors to make sure these are handled properly. For example, a destructor of class Comp, should Comp contain a dynamic array made through a pointer called mem, might look like this:

    Code:
    Comp::~Comp()
    {
         if (mem != NULL) 
         {
              delete [] mem;
         }
    }
    It is there that I have a little bit of a blind spot. I can't quite remember how to write those destructors correctly, and I'm having difficulty simply being able to look it up. I know that it looks something like that, but I don't remember the details. Can anybody fill me in?

    Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    if leaving the class aside, do you know how to dynamically allocate the memory, and then free it in a simple function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100

    memory question

    as in:

    Code:
    Comp* computer = new Comp;
    delete Comp;
    ?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    yes,
    so in your case new goes to the constructor
    and delete in the destructor

    the syntax of delete is not changed.

    for each new - you have to have a delete
    for each new[] - delete[]
    etc

    bwt it is
    Code:
    delete computer;
    you delete the allocated object, not all the class
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    100

    for instance?

    so for instance:

    Code:
    class Comp
    {
    public:
         Comp(int i = 5);
         ~Comp();
    
    private:
         int* mem;
         int** pix;
         int count;
    };
    
    Comp::Comp(int i)
    : count(i)
    {
         mem = new int[i];
         pix = new int*[i];
         
         for (int j = 0; j < i; j++)
         {
              pix[j] = new int[i];
         }
    }
    
    Comp::~Comp()
    {
         if (mem != NULL)
         {
              delete mem;
         }
    
         for (int i = 0; i < count; i++)
         {
              if (pix[i] != NULL)
              {
                   delete pix[i];
              }
         }
      
         if (pix != NULL)
         {
              delete pix;
         }
    }
    Is that the way it's supposed to work?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Yes, except that you can leave out the check for NULL:
    Code:
    Comp::~Comp()
    {
        delete mem;
    
        for (int i = 0; i < count; i++)
        {
            delete pix[i];
        }
    
        delete pix;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    100

    thanks

    Thank you much for this. Back when I remembered this somewhat, I still had a hard time grasping it. But the way you've explained it seems to helped a lot with long-term memorization and comprehension, not just short-term. Thanks.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Hold on, there's another thing. Notice:
    Code:
         mem = new int[i];
         pix = new int*[i];
         
         for (int j = 0; j < i; j++)
         {
              pix[j] = new int[i];
         }
    So, you use new[] for mem, pix and pix[j], thus you must use delete[] for them too:
    Code:
    Comp::~Comp()
    {
        delete[] mem;
    
        for (int i = 0; i < count; i++)
        {
            delete[] pix[i];
        }
    
        delete[] pix;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    100
    Ah, thank you.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you do it this way, you'll also need to write the copy constructor and overload the assignment operator to ensure that these operations work properly (or make them private to prevent copying/assigning).

    However, if you used a standard container, such as std::vector, instead of dynamically allocated arrays, you wouldn't even need to write a destructor, because those containers manage memory automatically for you.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    100
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 8
    Last Post: 04-28-2008, 02:46 AM
  3. vector destruction question
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 01:20 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 07:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21