Mail Archives: djgpp/2013/08/03/09:30:17
On Thu, 01 Aug 2013 08:47:14 -0400, DJ Delorie <dj AT delorie DOT com> wrote:
>> A NULL pointer can and does point to accessable memory for most C
>> compilers.
>
> It's not the compiler that decides this, it's the operating system.
No, it's the compiler. The compiler can implement NULL as a low address
or high address or anywhere inbetween, as long as it doesn't have the
same address as any existing C objects.
> Many operating systems do not map the first page of memory, [...]
A NULL is not required to be mapped to low memory. This is irrelevant.
NULL's actual value is not required to be zero, only different.
> [...] so a NULL
> pointer may not point to accessible memory.
True. It could also point at the start of a ROM etc, which could be
accessible during bootup but not later on. However, that has no
bearing on the fact that NULL typically "does point to accessible
memory for most C compilers".
>> Typically, this is pointer or address to memory location zero, but
>> it's not required to be. It can be *any* value as long as no other
>> C object has that same address.
>
> "An integer constant expression with the value 0, or such an
> expression cast to type void *, is called a null pointer constant."
>
> So, it's zero now.
This is C language semantics for NULL, not the actual implementation
value for NULL used by the compiler. I.e., a "0" in the code used as
specified represents NULL, but the NULL may actually be at an
address other than zero.
(No joke. You can ask even the angry nutjobs on c.l.c and get that
correct answer. Please read the two posts which I provided links to.
They're by one of the authors of the ANSI C specifications.)
>> If NULL's type is a void pointer, it is (losslessly) converted to a char
>> pointer when passed into a function for a char pointer argument. I.e.,
>> after the conversion, it functions as a char pointer in the function.
>
> "A string is a contiguous sequence of characters terminated by and
> including the first null character."
>
> So there's a difference between "char pointer" and "pointer to a
> string".
True.
Strings are "second class citizens" being implemented as arrays in C.
> atoi() takes a pointer to a string.
True. We were discussing how to make a NULL more useful by preventing
atoi() from failing when passed in. One method is for NULL to be not
just a "char pointer", but also be "a pointer to a string" - an empty
string.
> Not all char pointers are pointers to strings.
True.
(So far, I'm *really* unsure why you're attempting to educate me on the
definition of a C string ... Was this for the OP?)
> "If a null pointer constant is converted to a pointer type, the
> resulting pointer, called a null pointer, is guaranteed to compare
> unequal to a pointer to any object or function."
Exactly. That was stated by me in the other post and again above.
> Since a pointer to any valid string cannot be equal to the null
> pointer, a null pointer cannot point to a string.
Sorry, you've made a logic error here. The prior quote of yours only
requires the address used for the NULL to be unequal to any valid object
or function. You've implicitly defined your own term: "valid string",
as including the hypothetical null string that would be stored at NULL's
address. The "NULL null string object" would need to be excluded from
your definition of "valid", i.e., be present and useable as a null string,
but not considered to be "valid" since the system created it not the user,
i.e., just like NULL is defined as not valid.
Rod Pemberton
--
"... to secure these rights, governments are instituted among men,
deriving their just powers from the consent of the governed; that
whenever any form of government becomes destructive of these ends,
it is the right of the people to alter or abolish it ..."
-Thomas Jefferson, Declaration of Independence, 1776
- Raw text -