Pointer Question

This is a discussion on Pointer Question within the C++ Programming forums, part of the General Programming Boards category; Is there a need for removing pointers after use ptr = &c; Marky_Mark...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    42

    Pointer Question

    Is there a need for removing pointers after use


    ptr = &c;


    Marky_Mark

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You should only have to do anything if they point to data that you've dynamically allocated using new or malloc, in which case you should call delete/free() and then point them to NULL.
    zen

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Talking

    It depends. If you have allocated memory for that pointer using one of the following: malloc(), calloc(), realloc() or new you have to return that memory to the machine eventually - using either free() or delete.

    If you don't free the memory you allocated you will wind up with a memory leak, which is a very bad thing and is hard to find.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    I will be using new

    What's the correct syntax for freeing objects

    Marky_Mark

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    It would be...

    delete [] *iPointer;
    *iPointer = NULL;
    "Where genius ends, madness begins."
    -Estauns

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It depends on how they're allocated, if you use [] with new (ie create an array), then you should use [] in the delete statement -

    delete [] iPointer;

    or if you create a single object then just

    delete iPointer;

    is required.
    zen

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    My bad, should of clarified that.
    "Where genius ends, madness begins."
    -Estauns

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Thanks for clearing that up



    Marky_Mark

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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