Structure padding

Pekka Akselin [The Mad Midnight Hacker] pekka at paix.ikp.liu.se
Tue Apr 11 19:52:16 AEST 1989


In article <8392 at xanth.cs.odu.edu> kremer at cs.odu.edu (Lloyd Kremer) writes:
>In article <440 at front.se> zap at front.se (Svante Lindahl) writes:
>>Consider a struct consisting of a few character arrays, for example:
>>
>>	stuct foo {
>>	    char bar[3];
>>	    char baz[6];
>>	    char frotz[4];
>>	} fred;
>>
>>I need to know if I can safely assume that there will not be any
>>padding between the arrays.
[...]
>You would simply define one big array (a C array is guaranteed to be
>contiguous), and do the internal indexing yourself.  There should be no
>performance degradation since the compiler would arrange for such indexing
>anyway.  Also, some well-chosen macros could make using the strings easier.
[...]

How about this???

union {
	struct foo {
	    char	Bar[3];
	    char	Baz[6];
	    char	Frotz[4];
	} fred;
	char	SomeString[3 + 6 + 4];
} Strings;

#define bar	Strings.Bar
#define baz	Strings.Baz
#define	frotz	Strings.Frotz

Would there be padding between the Bar, Baz and Frotz arrays in this case?

My compiler generates the following line for the union above;

      .comm   _Strings,13

and the same for structure fred;

      .comm   _fred,13

Have a nice day!
-- 
Pekka					[The Mad Midnight Hacker Strikes Again]
_______________________________________________________________________________
pekka at paix.ikp.liu.se			...!uunet!enea!liuida!prodix!paix!pekka
Pekka Akselin, PA Mikroresurs, Sweden  (The Land Of The Mad Midnight Hacker 8-)



More information about the Comp.lang.c mailing list