constant pointers, a danger?

This is a discussion on constant pointers, a danger? within the C++ Programming forums, part of the General Programming Boards category; I have been rereading my C++ book to refresh my memory and I read a section of constant pointers. I ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    constant pointers, a danger?

    I have been rereading my C++ book to refresh my memory and I read a section of constant pointers. I know that to disarm stray pointers after calling 'delete' on them, you set them to null, that is the memory address of 0. However, constant pointers cannot have their address changed. Does that mean after I call 'delete' on a constant pointer, I now have a constant stray pointer? Can I even call 'delete' at all on constant pointers?

    My book didn't seem to answer this...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can call delete on a constant pointer -- but you can't change its value, as you mentioned, so you can't set it to NULL and you also can't use new to reuse the pointer, so it's just going to basically sit there and do nothing after that.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I'm finding it hard to imagine a case where it would be a problem.
    More often you have a pointer to a constant int:
    Code:
    const int *p
    than you have a const pointer to int:
    Code:
    int *const p;
    For the former it's not a problem, p=NULL is just fine. For the later it is almost certainly a local variable in which case just change the declaration to be non-const if you don't want to make things difficult for yourself.

    The real answer of course is that you should just use an auto_ptr (for example) anyway, and let it get cleaned up on its own when it goes out of scope.
    Last edited by iMalc; 09-30-2008 at 01:45 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Works for me. 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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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