[comp.sys.amiga] Byte/word/long alignment

john13@garfield.UUCP (John Russell) (01/12/88)

If I want to create an auto-initialized array of UBYTES, and then later
access each 4 bytes as a longword, can I ensure that the array is longword
aligned in some portable way (eg by placing it immediately after some other
32-bit global variable)? Will some compilers re-arrange declared storage
behind my back?

John

ewhac@well.UUCP (Leo 'Bols Ewhac' Schwab) (01/13/88)

In article <4366@garfield.UUCP> john13@garfield.UUCP (John Russell) writes:
>If I want to create an auto-initialized array of UBYTES, and then later
>access each 4 bytes as a longword, can I ensure that the array is longword
>aligned in some portable way (eg by placing it immediately after some other
>32-bit global variable)?

	Why don't you simply create it as an array of longwords, then access
it as bytes with a coerced pointer?

ULONG	foo[] = { 1, 2, 3, 4, 5, ...n };

bar()
{
	UBYTE	*bp;

	bp = (UBYTE *) foo;
	.
	.
	.
}

	No muss, no fuss, guaranteed alignment (if the compiler supports it).

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
Leo L. Schwab -- The Guy in The Cape	ihnp4!ptsfa -\
 \_ -_		Recumbent Bikes:	      dual ---> !{well,unicom}!ewhac
O----^o	      The Only Way To Fly.	      hplabs / (pronounced "AE-wack")
"Work FOR?  I don't work FOR anybody!  I'm just having fun."  -- The Doctor

cmcmanis%pepper@Sun.COM (Chuck McManis) (01/13/88)

In article <4366@garfield.UUCP> john13@garfield.UUCP (John Russell) writes:
!>If I want to create an auto-initialized array of UBYTES, and then later
!>access each 4 bytes as a longword, can I ensure that the array is longword
!>aligned in some portable way (eg by placing it immediately after some other
!>32-bit global variable)? Will some compilers re-arrange declared storage
!>behind my back?

The system call AllocMem() will guaruntee that the data is longword aligned.
The only hitch being that you have to remember to free it before you exit.


--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.

bobb@tekfdi.TEK.COM (Robert Bales) (01/14/88)

In article <4366@garfield.UUCP> john13@garfield.UUCP (John Russell) writes:

>If I want to create an auto-initialized array of UBYTES, and then later
>access each 4 bytes as a longword, can I ensure that the array is longword
>aligned in some portable way (eg by placing it immediately after some other
>32-bit global variable)?

Perhaps by using a union containing the array of UBYTES and a longword. Such
a technique is illustrated on page 173-174 of K&R. I say "perhaps" because I
don't know if a union can be auto-initialized. (A quick search of K&R didn't
yield the answer.)

"See you at AmiExpo"

   Bob Bales
   Tektronix, Inc.

I help Tektronix make their instruments. They don't help me make my opinions.

john13@garfield.UUCP (John Russell) (01/15/88)

In article <4974@well.UUCP> ewhac@well.UUCP (Leo 'Bols Ewhac' Schwab) writes:
>In article <4366@garfield.UUCP> john13@garfield.UUCP (John Russell) writes:

[ accessing bytes in an array as longs ]

>	Why don't you simply create it as an array of longwords, then access
>it as bytes with a coerced pointer?
>ULONG	foo[] = { 1, 2, 3, 4, 5, ...n };

Um, not ideal since the data makes much more sense as bytes -- it's bitmapped
definitions for the character set, and it helps to be able to check the
accuracy of the definitions visually (or at least it did when I was debugging
that section). I like to keep data in the most easily-interpreted form
possible, such as the binary-constant macros that I posted and use for things
like single-plane gadgets.

John

PS I ended up using a union, 8 bytes or 2 longs.
-- 
"Let's get those teabags outta here and replace them with some REAL Americans!"
			-- (Canadian) Paul Schaeffer has members of the British
			   nobility physically ejected from the Late Night crowd