Suggestions and question about bool pointers

This is a discussion on Suggestions and question about bool pointers within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Akkernight Now, I'd like suggestions on this and is there anyway to check if a pointer has ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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 01: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"

Page 2 of 2 FirstFirst 12
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, 10: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, 01:28 AM
  4. Architecture Question
    By Orborde in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2005, 09:05 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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