Article View: comp.std.c++
Article #1137Re: Question about sizeof in C/C++.
From: Andy Feibus
Date: Mon, 21 Dec 1992 13:54
Date: Mon, 21 Dec 1992 13:54
33 lines
1812 bytes
1812 bytes
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
Technical Details
Message-ID:
<cuX5VB1w164w@amfent.Gwinnett.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!gatech!emory!gwinnett!amfent!amfReferences:
<1992Dec18.021610.24482@nntpd.lkg.dec.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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