Bounds checks and a new question
Martin Weitzel
martin at mwtech.UUCP
Sat Dec 16 04:39:10 AEST 1989
In article <466 at cpsolv.UUCP> rhg at cpsolv.uucp (Richard H. Gumpertz) writes:
>In article <1989Dec12.190347.13521 at twwells.com> bill at twwells.com (T. William Wells) writes:
>>The two are exactly the same:
>>
>> &A[N] = &(*(A + N)) = A + N
>
>No, the two are not exactly alike. According to 3.3.6, *(A+N) is
>undefined ("...the behavior is undefined if the result is used as an
>operand of a unary * operator") and so &(*(A+N)) is undefined. A+N, on
>the other hand, is well defined. I really believe that the wording in
>3.3.6 is wrong; I cannot believe that the committee intended for A+N to
>be legal but not &A[N].
[rest deleted]
Given the fact, that the committe surely didn't want to break much
working code, I think &A[N] will stay as correct as it allways was.
Furthermore, IMHO it's not at all a difficulty for any implementor
of a compiler, because &(*(A+N)) must in any case be detected as
a special case at compilation time and should in no case require
any access to a memory location at distance N of A (which is, of
course, not allways defined).
But what should we, the community of C-programmers worry about?
Given there is any implementor, who takes the standard 'by the word'
and makes &A[N] an illegal construct, we would surely warn everyone
on the net, that this product is unusable and sooner or later it
will be changed. (Very optimistic view, I know :-/ ).
I have a related question on this topic. I'm teaching
courses on C and sometimes I use this little example:
main()
{
char a[10];
printf("%d %d\n", (int)a, (int)&a);
printf("%d %d\n", (int)(a+1), (int)(&a+1));
}
If the compiler doesn't consider "&a" to be an error (more on this
soon), running the program will print four numbers. The first two
are the same, because the first element of an array has the same
adress as the array in its total, but the third and fourth differ,
because the diffent type of "a" and "&a": By definition "a" is a
"pointer to char" (the arrays component type), but "&a" is a "pointer
to an array of 10 char", because for any "v" that is declared to
have type "T" "&v" has type "pointer to T". Since all adress
arithmetic is done scaled to the size of the pointed to object,
adding 1 to the same value ("a" or "&a") yields in a different
result!
As I allready said, I found this little example very instructive,
but with a change of compiler it suddenly was not any more "legal".
I could not even blame the compiler to be broken, because somewhere
in K&R there is a statement, that it is not allowed to take the adress
of an array, because the name of an array without following "[]" is
a constant and you cannot take the adress of a constant.
A few days ago I had the chance to try this with Turbo-C, the example
compiled there without error, but the behaviour was *different*
depending on "a" beeing static or auto!!
So, what does the pANS say?
Legal, Illegal, Implementation defined, Undefined ....
--
<<< MW -- email: see header -- voice: 49-(0)6151-6 56 83 >>>
More information about the Comp.std.c
mailing list