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: 103376,c9c309a1859318a1 X-Google-Attributes: gid103376,public X-Google-Thread: 114917,76b1fcc14e8dced X-Google-Attributes: gid114917,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: <33b67602.54296884@nntp.ix.netcom.com>#1/1 X-Deja-AN: 253432073 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> Organization: Netcom X-NETCOM-Date: Sun Jun 29 8:05:18 AM PDT 1997 Newsgroups: comp.lang.c,comp.lang.c++,comp.os.msdos.programmer,comp.lang.asm.x86,comp.lang.ada Date: 1997-06-29T08:05:18-07:00 List-Id: dewar@merv.cs.nyu.edu (Robert Dewar) wrote: > Michael says > > < correct. Objects may be padded in that there may be bytes that are > not used, but there must be no additional padding between array > elements. For any type T, > > T a; > T b[5]; > > we must have sizeof b == 5 * sizeof a. There can be no unused space > between b[0] and b[1]. > >> > > Ah, a confusion. I thought the statement was about Ada. > > But I am interested in how you could construct a valid ANSI C program > with portable semantics that could detect the difference, and in particular > how exactly the ANSI C standard formally prohibits such "additional padding". > This is the sort of requirement that is very easy to state informally, but > much harder to state formally. > > What would one expect on a PDP10 with 5 characters per word and an unused > bit left over, or does the ANSI C standard require 36 bits/character on > this machine? > > < could quote chapter and verse on this>> > > This sort of statement is the kind of statement that Ada would certainly > like to make in the packed case, but it is hard to make it more than > implementation advice. > > I also don't see how your conditoin above can be correct > > suppose that type T is a struct with a double field and an int field. > The size of this struct would be 12 bytes, but its alignment requirement > is 8, so it would not be possible to laout the array b without alignment > gaps??? > It's quite easy to detect the difference. For example, #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. Note also that without this rule there would be no guarantee that code like #include int a[5] = { 1, 2, 3, 4, 5 };; int *b = malloc(5 * sizeof *b); if (b != NULL) memcpy(b, a, sizeof a); would work. It would be extremely difficult for an implementation to fake this with non-contiguous arrays since the ultimate type of the allocated memory may not be known at the time the malloc() is done. >From ISO 6.1.2.5: An array type describes a contiguously allocated nonempty set of objects ... This rule is not needed in languages like Ada in which the allocation can be done by statements or functions that have access to the type being allocated. PL/I also does not have this requirement and, in fact, one does occasionally have arrays with non-contiguous elements. Michael M Rubenstein