From 8715805071829158251
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1149ec,c0f8f764e8b7d30f
X-Google-Attributes: gid1149ec,public
X-Google-Thread: f78e5,c0f8f764e8b7d30f
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1993-01-01 08:35:18 PST
Xref: sparky comp.std.c++:1946 comp.std.c:3311
Path: sparky!uunet!think.com!sdd.hp.com!cs.utexas.edu!rutgers!twwells!bill
From: bill@twwells.com (T. William Wells)
Newsgroups: comp.std.c++,comp.std.c
Subject: Re: Question about sizeof in C/C++.
Message-ID: <C06HBn.Mr6@twwells.com>
Date: 1 Jan 93 13:54:52 GMT
References: <wwJXVB3w164w@amfent.Gwinnett.COM> <1992Dec24.001540.30000@microsoft.com> <jamesc.725854759@bart>
Organization: None, Mt. Laurel, NJ
Lines: 30

In article <jamesc.725854759@bart> jamesc@swapsdev.state.state.COM.AU (James Cribb) writes:
: jimad@microsoft.com (Jim Adcock) writes:
: >jamesc@swapsdev.state.state.COM.AU (James Cribb) writes:
: >| Given
: >|     struct Something*  ps;
: >|     unsigned  k;
: >| do the C or C++ standards guarantee that
: >|     &ps[k] == ((char*) ps) + k * sizeof(struct Something)
: >
: >No.  The relationship holds only if ... k is less than or equal
: >to the number of elements in the array pointed-to by ps
:
: My code meets those conditions, but just out of interest,
: if k is too big, what could cause an inequality (of the addresses)?

In the compiler I've written, if an object is known to be less
than 64K bytes, its index arithmetic is done with 16 bits but all
pointer arithmetic is done with 32 bits. So, if sizeof(*ps) * k
is over 64K, the pointer arithmetic will create a different
result from the index arithmetic.

(Well OK, for this particular example, the compiler is actually
smart enough to see the equivalence between the index expression
and the pointer addition and so the equality would hold. But if
the (char*)ps weren't detectable as an object pointer, they
wouldn't be equal.)

---
Bill                            { rutgers | decwrl | telesci }!twwells!bill
bill@twwells.com


