[comp.lang.c] Is range

flee@blitz.cs.psu.edu (Felix Lee) (07/28/88)

Harbison&Steele (2e) says (p. 156)
> Some implementations of C use int or long int as the type of the
> result of sizeof, but this is an inferior choice because it may be
> impossible to represent the size of a very large array, say one that
> spans more than half the total address space.

But such a large array breaks ptrdiff_t.  Is ptrdiff_t required to
have a larger range than size_t?  Can you stuff a size_t value into a
ptrdiff_t variable without overflow?  Is the following relationship
always true?
	sizeof(char a[]) == &a[sizeof(a)] - &a[0]
--
Felix Lee	*!psuvax1!flee

pk-tle@nada.kth.se (Tommy Levitte) (07/29/88)

> ....
>Is the following relationship always true?
>
>	sizeof(char a[]) == &a[sizeof(a)] - &a[0]

No! If you use any C on a PC system, this will never be true, due to the
segment:offset adress format.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/31/88)

In article <485@draken.nada.kth.se> pk-tle@draken.UUCP (Tommy Levitte) writes:
>>Is the following relationship always true?
>>	sizeof(char a[]) == &a[sizeof(a)] - &a[0]
>No! If you use any C on a PC system, this will never be true, due to the
>segment:offset adress format.

Yes!  At least, modulo correcting the syntax of the example,
C requires the relationship to be true.

On a segmented architecture, if the array is allocated within
a single segment, it is easy to ensure this.  Otherwise, the
compiler will have to use more involved pointer operation
support, but it's still required that the difference of two
pointers into an array yield the number of array elements
between them (give or take 1).

If your compiler doesn't do this, it is broken.

mouse@mcgill-vision.UUCP (der Mouse) (08/11/88)

In article <485@draken.nada.kth.se>, pk-tle@nada.kth.se (Tommy Levitte) writes:
>> Is the following relationship always true?
>>      sizeof(char a[]) == &a[sizeof(a)] - &a[0]
> No! If you use any C on a PC system, this will never be true, due to
> the segment:offset adress format.

I believe that ANSI C will require (ie, draft ANSI C does require) that
this be true, and that the compiler arrange whatever is necessary when
subtracting pointers to make it so.

What's this about segment:offset address format?  My idea of a PC is a
MicroVAX-II, at least.  (Oh, you mean *IBM* PC?  Shoulda said so.)

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu