🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #13461

Re: Byte .vs. Word Alignment

#13461
From: marcus
Date: Wed, 22 Apr 1987 19:41
37 lines
2243 bytes
In article <244@petro.UUCP> jrb@petro.UUCP (Jon Boede) writes:
>For example, assuming that a structure like:
>
>	struct {
>		int an_int;
>		char a_char;
>	} a_struct;
>
>starts on a word boundry, would a structure placing the /char/ first and the
>/int/ therefore a 1/4 word off be less, can we say, "efficient" in terms of
>the manipulation needed to get to /an_int/??  Or doesn't the C compiler always
>start structures on words boundries, making alignment consideration by the
>programmer meaningless?
>
>	struct {
>		char a_char;
>		int an_int;
>	} a_struct;	/* is this less "efficient"? */

Most compilers would probably pad between a_char and an_int in the second
example so that an_int would be at a word boundary.  It is worth noting that
even in the first example, most compilers will pad 3 bytes to the end of
the structure so that in either case the structure will have a size of 8
bytes (68000 => 32 bit ints).  This is done so that if you had an array
of a_structs, each one would begin at an aligned address.  This seems odd
at first, but after sufficient consideration starts to make sense.

As far as efficiency of referencing an_int is concerned, the first case is
probably going to be more efficient, but not because of alignment.  The
first element in a structure can usually be accessed easier than any other
elements because its offset in the structure is 0 and the compiler can
probably generate simplified addressing modes to get to it.

Marcus Hall
..!{ihnp4,mcdchg}!illusion!marcus
	The illusion is complete, it is reality, the reality
	is an illusion, and the ambiguity is the only truth.
Technical Details
Message-ID: <145@illusion.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!seismo!gatech!mcdchg!illusion!marcus
References: <244@petro.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!seismo!gatech!mcdchg!illusion!marcus
From: marcus@illusion.UUCP
Newsgroups: comp.arch,comp.lang.c
Subject: Re: Byte .vs. Word Alignment
Message-ID: <145@illusion.UUCP>
Date: Wed, 22-Apr-87 14:41:06 EST
Article-I.D.: illusion.145
Posted: Wed Apr 22 14:41:06 1987
Date-Received: Fri, 24-Apr-87 03:45:28 EST
References: <244@petro.UUCP>
Reply-To: marcus@illusion.UUCP (Marcus Hall)
Organization: Magic Numbers Software, Bloomingdale, IL
Lines: 37
Keywords: C, 680X0, Alignment