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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC 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: 109fba,76b1fcc14e8dced X-Google-Attributes: gid109fba,public X-Google-Thread: 1014db,8b6c45fbebd7d3b7 X-Google-Attributes: gid1014db,public X-Google-Thread: 103376,c9c309a1859318a1 X-Google-Attributes: gid103376,public X-Google-Thread: 114917,76b1fcc14e8dced X-Google-Attributes: gid114917,public From: hook@cscsun3.larc.nasa.gov (Ed Hook) Subject: Re: HELP ! need to insert value in array !! Date: 1997/06/30 Message-ID: <5p8bd0$rhc$2@reznor.larc.nasa.gov>#1/1 X-Deja-AN: 253613323 Distribution: world 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: CSC/NASA Langley Research Center Reply-To: hook@cscsun3.larc.nasa.gov Newsgroups: comp.lang.c,comp.lang.c++,comp.os.msdos.programmer,comp.lang.asm.x86,comp.lang.ada Date: 1997-06-30T00:00:00+00:00 List-Id: In article , dewar@merv.cs.nyu.edu (Robert Dewar) writes: |> Michael said |> |> < |> #include |> |> int main(void) |> { |> int a[2]; |> unsigned long d = (char*) a[1] - (char*) a[0]; Actually, the line above should probably be: unsigned long d = (char *)(a + 1) - (char *)a; |> 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? |> The compiler is allowed to insert padding bytes _in_the_element_; it's just not permitted to add additional padding _between_ elements. That's needed to guarantee (for instance) that an allocation of n * sizeof() bytes can successfully hold an array of n elements of type . |> Or are you saying that the ANSI standard requires that the size be a .allows. |> multiple of the required alignment. If so, please quote chapter and |> verse. |> I don't have a copy of the Standard handy, so I'll let someone else provided citations. |> 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. |> I'm fairly certain that this (=="no padding between array elements") _is_ guaranteed by the Standard (but, again, I can't provide the requested "chapter and verse"). |> if the latter, relevant references to the standard would be helpful in |> following your argument. |> -- Ed Hook | Copula eam, se non posit Computer Sciences Corporation | acceptera jocularum. NASA Langley Research Center | Me? Speak for my employer?...<*snort*> Internet: hook@cscsun3.larc.nasa.gov | ... Get a _clue_ !!! ...