Size of structure containing char fields 
Author Message
 Size of structure containing char fields

Given a structure that contains only char fields (possibly unsigned):

        struct example1 { char a, b, c; };

is ANSI restrictive enough [;-)] to force sizeof(example1) to be 3?

Is anyone aware of existing compilers for which this wouldn't be true?

Is there a portability problem with the following structure where the
array is intended to support a variable length array?

        struct example2 { char a, b, c[1]; };

Thanks in advance.



Sat, 02 Jan 1993 22:29:50 GMT  
 Size of structure containing char fields

Quote:

>Given a structure that contains only char fields (possibly unsigned):

>    struct example1 { char a, b, c; };

>is ANSI restrictive enough [;-)] to force sizeof(example1) to be 3?
>Is anyone aware of existing compilers for which this wouldn't be true?

ANSI more or less leaves the question open.  Machines on which char
pointers are strange will almost certainly pad the structure to a word
boundary so they can use "normal" pointer format for `struct example1 *'.
Some compilers on more ordinary machines may do likewise.  Some won't.
--
NFS:  all the nice semantics of MSDOS, | Henry Spencer at U of Toronto Zoology



Sun, 03 Jan 1993 23:25:00 GMT  
 Size of structure containing char fields

Quote:

>Given a structure that contains only char fields (possibly unsigned):

>    struct example1 { char a, b, c; };

>is ANSI restrictive enough [;-)] to force sizeof(example1) to be 3?

No. The compiler is permitted to put in padding between members and
at the end "as necessary to achieve the appropriate alignment were the
structure or union to be an element of an array" (section 3.5.2.1).
To do what you want, the standard would have to constrain implementors
to do this and no more, but it seems that the constraint is only that
it be sufficient, at least as I read it.

Quote:
>Is anyone aware of existing compilers for which this wouldn't be true?

Sun's C compiler on a Sun 3 (with Sun OS 4.0) aligns structs on two
byte boundaries. This is NOT an ANSI-compatible compiler, but I don't
think that the ANSI standard will force them to change their ways. My
guess is that the implementation was done this way simply because it
was easier. Two-byte alignment is sufficient for any data type on the
Sun 3, and rarely wasts any storage since only char datatypes can get
by with only 1-byte alignment (your example is an exception).
Interestingly, with the Sun 4, your struct in fact has a size of 3,
even though the Sun 4 generally has more strict alignment rules than
the Sun 3.

Another compiler that uses a constant 2-byte alignment rule is the
Data General MV-series C compiler. Since the MV is a word-addressed
machine, this is the most reasonable implementation and permits struct
pointers to be word pointers. (Word and Character pointers have
different formats on the MV). Permitting an odd-sized struct would
require using character pointers instead of word pointers to structs,
which is less efficient in most instances.

Quote:
>Is there a portability problem with the following structure where the
>array is intended to support a variable length array?

>    struct example2 { char a, b, c[1]; };

I think this approach is reasonably portable if you use offsetof
instead of relying on the sizeof() operator. E.g. to allocate the
structure allowing the array "c" to be "n" bytes long:

   #include <stddef.h>
   #include <stdlib.h>
   ...
   struct example2 { char a, b, c[1]; };
   struct example2 *ptr = malloc(offsetof(struct example2, c) + n);
--
Jim Patterson                              Cognos Incorporated
UUCP:decvax!utzoo!dciem!nrcaer!cognos!jimp P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4



Sat, 09 Jan 1993 03:42:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. structure containing pointer to char over the network

2. Marshaling a structure containing a aray of different structures

3. Size of text-fields/edit-fields depending on Font selected

4. Differences between char array[SIZE] and char *array

5. Find size of variable size structure?

6. Pointers to Structure that contains pointer to Function

7. printing a string contained in a structure

8. reading a structure containing pointers

9. Sorting a last names that are contained within a structure

10. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

11. A question about a structure containing ':'

12. self contained pointers to structures

 

 
Powered by phpBB® Forum Software