Thread: ***How to delete a pointer***

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Question ***How to delete a pointer***

    Hi friends,

    Can anyone tell me how to delete a double pointer ,means a pointer which contains the address of another pointer?


    Thanks in Advance!!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on where the pointer points to and what happened previously. Anything that came from new should be delete'd -- that means that if you created (say) a faux two-dimensional array with new in a loop, you'll have to delete in a loop.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Dereference it to delete whatever is pointed at.

    For example;
    Code:
    int main()
    {
        int **x = new (int *);
        *x = new int [5];
          // now delete things in reverse order
        delete [] (*x);   // brackets () for clarity
        delete x;
    }
    As always, nothing should be delete'd unless created using the corresponding operator new.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    In two 2D array first delete the members and then the main pointer which is pointing towards the pointer to an array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM