Thread: Suggestions and question about bool pointers

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Quote Originally Posted by Akkernight View Post
    Now, I'd like suggestions on this and is there anyway to check if a pointer has been assigned anything else than 'garbage' ? Like, if it has been assigned by the code, and not some default compiler thing...
    No. There is not a way to do that.
    Given an arbitrary value for a pointer, you can certainly check if it happens to be a value close to NULL, or equal to a compiler filled value like 0xCCCCCCCC, and if you really get your hands dirty, you can with a high degreee of accuracy tell if is points to within the stack vs the heap. However, none of this will EVER guarantee you that it points to something valid.

    You must never write a program such that you have to determine afterwards whether a pointer value is valid or not. You must always already know whether it is valid or not, and this often means tracking it yourself. Commonly we track an invalid pointer by setting it to NULL. Other ways might be to have a decicated boolean variable alongside the pointer.
    If you ignore this advice you're going to screw yourself.
    Last edited by iMalc; 03-22-2009 at 12:32 PM.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for this code? and 1 question....
    By Huskar in forum C Programming
    Replies: 6
    Last Post: 03-31-2009, 09:24 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 12:28 AM
  4. Architecture Question
    By Orborde in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2005, 08:05 AM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM