M
Mabden
Keith Thompson said:Mabden wrote: [...]I still have a question as to whether an automatic pointer (in a
function, initialized by the compiler) is set to all-bits-zero or to the
address of NULL (void *(0)). If pointers are init'ed to zero as other
automatic variables are in a function, and NULL points to somewhere else
in memory aren't they different?
I think he was actually asking about the implicit initialization of
automatic variables. The answer, of course, is that there is none.
I was actually asking about whichever pointers are initialized to
something by the compiler, and whether that something was all-bits-zero
or NULL. Kuyper says they init'ed to NULL, but that there may be more
than one NULL (which I had never heard of before). Of course we all know
NULL may or may not be at location zero, so let's not revisit that
again.
To expand on that a little:
The current value of a pointer object is the value most recently
assigned to it, or the value with which it was initialized if it's
never been assigned to. (I'm assuming "assigned to" refers to any
method of modifying the value, including memcpy(); I'm also ignoring
any issues involving volatile objects.) The same is true of objects
of any type; there's nothing special about pointers here.
If the current value of a pointer object is a null pointer, assigning
it to a _Bool object assigns the value 0 (false). If the current
value is anything other than a null pointer, assigning it to a _Bool
object assigns the value 1 (true). If the pointer object is
uninitialized, which can happen if it's an automatic variable with no
explicit initialization, then any attempt to refer to its value
invokes undefined behavior; attempting to assign this uninitialized
value to a _Bool object can do anything. (It's likely to assign
either 0 or 1, but it could, for example, crash the program as soon as
the pointer value is accessed.) This can and should be avoided by
consistently initializing all variables (explicitly or implicitly)
before using them.
So it appears it is not so random as it first looked to me. I don't take
the uninititialized case into much account, since I initialize things at
all times, and I would consider that case a programmer error.
I was worried about moving working code to another platform, or even a
new version of a compiler, and having it break. Mainly cases like a
pointer set to all-zeroes being seen as a false in the case where it is
a valid location for some piece of hardware, and should be a true.
That said, I still don't see a very compelling reason to store the true
/ false value of a pointer, rather than just testing the pointer itself.
Therefore, if I may refer back to your comment that started this bit of
the thread, I find setting a _Bool to a pointer "uninteresting".
I apologize to all that this thread went on so long, but sometimes these
fine points can't be easily explained, and I find that I must be
explaining my point in Martian or something, sometimes. (Well, being an
offensive bastard probably doesn't encourage people in wanting to help
me out, either)
I hope we've resolved this. I am satisfied, at least.