From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f6aa,76b1fcc14e8dced X-Google-Attributes: gid10f6aa,public X-Google-Thread: 1014db,8b6c45fbebd7d3b7 X-Google-Attributes: gid1014db,public X-Google-Thread: 114917,76b1fcc14e8dced X-Google-Attributes: gid114917,public X-Google-Thread: 103376,c9c309a1859318a1 X-Google-Attributes: gid103376,public X-Google-Thread: 109fba,76b1fcc14e8dced X-Google-Attributes: gid109fba,public From: miker3@ix.netcom.com (Mike Rubenstein) Subject: Re: HELP ! need to insert value in array !! Date: 1997/06/29 Message-ID: <33bec9db.75762751@nntp.ix.netcom.com>#1/1 X-Deja-AN: 253484937 References: <33A9C27C.22F7@post4.tele.dk> <5oci49$97g@crl.crl.com> <866920621snz@genesis.demon.co.uk> <5p0v7l$9uc@nntp.seflin.org> <33b64c2f.43589878@nntp.ix.netcom.com> <33b67602.54296884@nntp.ix.netcom.com> Organization: Netcom X-NETCOM-Date: Sun Jun 29 4:16:10 PM CDT 1997 Newsgroups: comp.lang.c,comp.lang.c++,comp.os.msdos.programmer,comp.lang.asm.x86,comp.lang.ada Date: 1997-06-29T16:16:10-05:00 List-Id: dewar@merv.cs.nyu.edu (Robert Dewar) wrote: > Michael said > > < > #include > > int main(void) > { > int a[2]; > unsigned long d = (char*) a[1] - (char*) a[0]; > printf ("%lu %lu\n", d, sizeof(int)); > return 0; > } > > This must print the same number twice. > >> > > > I am unable to prove that the above code has well defined semantics > according to the ANSI standard. The casts of integer pointers into > character pointers seem quite implementation dependent in their > exact meaning to me. In fact it seems quite hard yto implement > this natural semantics on some word oriented machines. > > Also, you did not answer my point about alignment. Suppose the array > element type is one whose size is not a multiple of its alignment. > How can gaps be avoided in such a case? > > Or are you saying that the ANSI standard requires that the size be a > multiple of the required alignment. If so, please quote chapter and > verse. > > It would be useful to know whether you are talking about things that "everyone > knows must be so", or things that are guaranteed to have the semantics you claim > from the standard. > > if the latter, relevant references to the standard would be helpful in > following your argument. > I am talking about things that are guaranteed in the standard. Obviously, they are not things that "everyone knows must be so". I already quoted the relevant passage -- the elements of an array are contiguous (ISO 6.1.2.5). If you don't like pointer conversion, consider the program int main() { int a[10]; int b; print("%lu %lu\n", (unsigned long) sizeof a, 10 * (unsigned long) sizeof b); return 0; } No pointer conversion here, but since an array consists of contiguous elements, this must print the same number twice. If the elements were not contiguous, it would print different numbers. While footnotes are not part of the standard, you might consider footnote 46 in 6.3.6: Another way to approach pointer arithmetic is first to convert the pointer(s) to character pointer(s). In this scheme, the integral expression added to or subtracted from the converted pointer is first multiplied by the size of the object originally pointed to, and the resulting pointer is converted back to the original type. For pointer subtraction, the result of the difference between the character pointers is similarly divided by the size of the object originally pointed to. Also consider the example (note, however, that examples are not part of the standard) in 6.3.4: 2. Another use of the sizeof operator is to compute the number of elements in an array: ` sizeof array / sizeof array[0] Michael M Rubenstein