[comp.lang.c] Order of fields

jss@hector.UUCP (Jerry Schwarz) (09/29/87)

In article <1044@ius1.cs.cmu.edu> edw@ius1.cs.cmu.edu.UUCP writes:
>
>   Let me give you a reason of why this should not be "kosher".
>
>  	    I don't think it is  required in C that the fields in a structure
>	    be phyically ordered in the same ordering they are declared.

But it is.  The version of the ANSI standard I have at hand (slightly
out of date but probably unchanged on this point) says(section
3.5.2.1):

    Within a structure object, the non-bit-field members and the
    units in which bitfields reside have addresses that increase in
    the order in which they declared.

K&R said the same thing in different words.

Jerry Schwarz

edw@ius1.cs.cmu.edu (Eddie Wyatt) (10/01/87)

> 
> But it is.  The version of the ANSI standard I have at hand (slightly
> out of date but probably unchanged on this point) says(section
> 3.5.2.1):
> 
>     Within a structure object, the non-bit-field members and the
>     units in which bitfields reside have addresses that increase in
>     the order in which they declared.
> 
> K&R said the same thing in different words.
> 
> Jerry Schwarz

   Sorry, the correct terminology I should have used is "member" not "field".

  As two people have pointed out, both K&R and the ANSI standard do
dictate the the physical ordering of members of a structure.

  For those of you that complain about the size being unknown, I would
say that the size of struct { char x; int open[]; } should be
equal to the size of char + the number of bytes needed to align open
on a valid integer boundary.

   I still have two complaints about what you are doing:

  1. you can not validly pass by value  the structure you have allocated.
     (you loose the array part).

  2. what you are doing is a little obscure.

    There are posible alternatives to approximate what you need - declare
the struct as follows :

	struct foo {char  x; int open[MAX_OPEN_SIZE] }
	    zz = (struct foo *) malloc(sizeof(struct foo));

	     		or

	struct foo {char x; int *open}
		
	    zz = (struct foo *) malloc(sizeof(struct foo));
	    zz->open = (int *) malloc(sizeof(int)*7);


-- 

					Eddie Wyatt

e-mail: edw@ius1.cs.cmu.edu