Thread View: comp.std.c
4 messages
4 total messages
Started by Scott Mayo
Sat, 06 Apr 1996 15:02
Is it legal to add 0 to NULL? What about NULL < NULL?
Author: Scott Mayo
Date: Sat, 06 Apr 1996 15:02
Date: Sat, 06 Apr 1996 15:02
18 lines
1014 bytes
1014 bytes
I ask because a standard trick for walking an array A of size S is:
Thing *a = A;
Thing *fence = a + S;
for (; a < fence; ++a)
/* use a */;
but in my case, if S happens to be 0, a will start out NULL. This makes we
worry about both the value of fence, and, if that works, the behaviour
of (a < fence), which I hope is (NULL < NULL), which I hope the standard
blesses as reliably false, but I have this feeling that it won't.
I know I can use int i; for (i = 0; i < S; ++i) but I was
hoping to avoid the extra math at array dereference time. I know
I can preface the whole thing with if (a != NULL) but the fun of
a standard is knowing what you can get away with. :)
Comments?
Re: Is it legal to add 0 to NULL? What about NULL < NULL?
Author: Lawrence Kirby
Date: Sun, 07 Apr 1996 19:34
Date: Sun, 07 Apr 1996 19:34
54 lines
2352 bytes
2352 bytes
In article <ff1d.smail.smayo@tiac.net> smayo@tiac.net "Scott Mayo" writes:
>I ask because a standard trick for walking an array A of size S is:
>
>Thing *a = A;
>Thing *fence = a + S;
>for (; a < fence; ++a)
> /* use a */;
>
>but in my case, if S happens to be 0, a will start out NULL. This makes we
>worry about both the value of fence, and, if that works, the behaviour
>of (a < fence), which I hope is (NULL < NULL), which I hope the standard
>blesses as reliably false, but I have this feeling that it won't.
The standard says about addition 6.3.6:
"If both the pointer and the result point to elements of the same array
object, or one past the last element of the array object, the evaluation
shall not produce an overflow; otherwise the behaviour is undefined"
Unfortunately a null pointer doesn't point to an array object so we
invoke 'otherwise'.
Similarly comparison against a null pointer (even another one) is defined
for equality operators == and != but not for relational operators < <= > >=.
If S can't be negative you could use:
>for (; a != fence; ++a)
But if you've already tested for S being zero, it probably doesn't help.
As far as efficiency is concerned you could write:
if (S > 0) {
Thing *a = A;
Thing *fence = a + S;
do {
...
} while (++a < fence);
}
>I know I can use int i; for (i = 0; i < S; ++i) but I was
>hoping to avoid the extra math at array dereference time.
Any reasonable conpiler should be able to optimise that to a pointer as
appropriate. It might even do a better job at optimising that than with the
code above.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Re: Is it legal to add 0 to NULL? What about NULL < NULL?
Author: Mark Brader
Date: Mon, 08 Apr 1996 05:30
Date: Mon, 08 Apr 1996 05:30
19 lines
1173 bytes
1173 bytes
> > Thing *a = A;
> > Thing *fence = a + S;
> > for (; a < fence; ++a)
> > /* use a */;
> >
> > but in my case, if S happens to be 0, a will start out NULL.
Er, no. If S happens to be 0, but A yields a valid Thing * pointer,
then a and fence will both start out equal to that pointer value.
If A is a null pointer or null pointer constant, *then* a will start
out as a null pointer, and the undefined behavior described by Lawrence
Kirby in the Referenced article will occur.
--
Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
"I'm a little worried about the bug-eater," she said. "We're embedded
in bugs, have you noticed?" -- Niven, "The Integral Trees"
My text in this article is in the public domain.
Re: Is it legal to add 0 to NULL? What about NULL < NULL?
Author: Peter Miller
Date: Tue, 09 Apr 1996 05:34
Date: Tue, 09 Apr 1996 05:34
14 lines
931 bytes
931 bytes
In article <ff1d.smail.smayo@tiac.net>, smayo@tiac.net (Scott Mayo) wrote: > I ask because a standard trick for walking an array A of size S is: > > Thing *a = A; > Thing *fence = a + S; > but in my case, if S happens to be 0, a will start out NULL. This makes we > worry about both the value of fence I don't think that is correct: If "a" is set to point to array A, then, after adding zero, it will still point to array A (unless A was a pointer that happended to be NULL). Peter
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads