🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1189

Re: Question about sizeof in C/C++.

#1189
From: T. William Wells
Date: Fri, 01 Jan 1993 13:54
30 lines
1750 bytes
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
Technical Details
Message-ID: <C06HBn.Mr6@twwells.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!sdd.hp.com!cs.utexas.edu!rutgers!twwells!bill
References: <wwJXVB3w164w@amfent.Gwinnett.COM> <1992Dec24.001540.30000@microsoft.com> <jamesc.725854759@bart>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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