Re: FW: [iPAQ] skiff tools question

From: Gordon McNutt <gmcnutt.a.t.ridgerun.com>
Date: Fri Sep 15 2000 - 13:06:08 EDT

Gordon McNutt wrote:

> Nicolas Pitre wrote:
>
> > On Thu, 14 Sep 2000, Gordon McNutt wrote:
> >
> > > George France wrote:
> > >
> > > > Correction:
> > > >
> > > > struct foo_t { u16 x; } __attribute__ ((packed));
> > > >
> > > > Best Regards,
> > > >
> > > > --George
> > >
> > > I hope it's something that simple. But the following example makes me
> > > worry that it is not, as this example does not use structs:
> > >
> > > u8 buf[4];
> > > u16 *a;
> > > a = &buf[1];
> > > *a = 4;
> >
> > This is the perfect example of what you can't do on any ARM processor (nor
> > Alpha nor IA-64, etc.) because alignment is important. Many applications
> > were written with i386 CPUs in mind where alignment isn't an issue, but
> > this is bad practice and not portable.
> >
> > Nicolas
>
> I think I have a solution that will work for me:
>
> struct u16_t { u16 a ; } __attribute__ ((packed));
> ...
> u8 buf[4];
> struct u16_t *a ;
> a = (struct u16_t*)&buf[j];
> a->a = 4;
>
> I can't use the __attribute__ directives on a built-in type or a typedef
> like u16 (if I understand the gcc documentation correctly), but I can wrap a
> type in a struct and apply the directive to the struct. Then, the ARM code
> works exactly like the i386. This works regardless of wether j is odd or not.
>

Thanks to all the folks who kindly responded. Yes, the solution was as simple as
adding the __attribute__ ((packed)) directive to ALL structs used to overlay --
not just the ones with an odd number of bytes (because the authors expected
these structs to follow one another contiguously). Now I just have to figure out
an easy way to port this all to a big-endian processor... ;)

--Gordon
Received on Fri Sep 15 10:06:12 2000

This archive was generated by hypermail 2.1.8 : Tue May 04 2004 - 09:43:42 EDT