From 1219032921093583220
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: 1992-12-22 01:54:50 PST
Xref: sparky comp.std.c++:1893 comp.std.c:3255
Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!gatech!emory!gwinnett!amfent!amf
From: amf@amfent.Gwinnett.COM (Andy Feibus)
Newsgroups: comp.std.c++,comp.std.c
Subject: Re: Question about sizeof in C/C++.
Message-ID: <cuX5VB1w164w@amfent.Gwinnett.COM>
Date: Mon, 21 Dec 92 08:54:59 EST
References: <1992Dec18.021610.24482@nntpd.lkg.dec.com>
Lines: 34

Resending this, since my upstream feed seems to be having problems....

diamond@jit345.bad.jit.dec.com (Norman Diamond) writes:
> In article <wwJXVB3w164w@amfent.Gwinnett.COM> amf@amfent.Gwinnett.COM (Andy F
> >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)
> >Assuming that you [cm]alloc enough memory for ps to support accessing the
> >k'th element of it, the answer is... no.
> He was asking about the standards, for which (under the condition you state)
> the answer is... yes.
Thanks for quoting me out of context, but he also asked:

	I'm asking because I have C++ code that relies on this.
	It works on Suns but is it portable?

And that was the question I answered.

> >Different compilers see &ps[k] differently; use parentheses to clarify:
> > &(ps[k]).
> Even K&R-1 and Stroustrop-1 prohibited seeing this expression differently.
That's nice in theory, but I've found at least one UNIX-based compiler 
that interprets it differently.  Since his question was about 
portability, I told him what to do to make sure it worked on 
whatever compiler he chose.  Of course it's not standard, but
complying with the standard and being portable can sometimes be
two very different animals.

-- Andy.
andyfe@utoday.com



