EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  <20042005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  <20042005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: RE: Compiler switches on RISC byte alignment
From: "Fairley, Diane" <[email protected]>
To: "Andrew Johnson" <[email protected]>, "Dalesio, Leo" <[email protected]>
Cc: <[email protected]>
Date: Thu, 2 Sep 2004 11:48:12 -0700
Hello Andrew,

I tried the gcc packed attribute as below (and individually on structure member):

typedef struct __attribute__ ((packed)){
     char           ch;      
     unsigned char  uCh;    
     short          sInt;   
     unsigned short usInt; 
     long           lInt ;   
     unsigned long  ulInt;   
     float          flt;    
} testdata_ts; /* 18 bytes */ 

On solaris-sparc, and I've found that it does pack the structure (sizeof(testdata) reports size of 18), but when trying to access an unaligned structure member, or point to one, the value is incorrect. Even a simple

printf("testdata.lInt = %lu", testdata.lInt)

does not print the right value.  A pointer to an unaligned structure member results in a segment violation.

Is there further attributes (or compiler switch) needed in order to access packed structure members?

Diane Fairley



-----Original Message-----
From: Andrew Johnson [mailto:[email protected]] 
Sent: Wednesday, September 01, 2004 10:23 AM
To: Dalesio, Leo
Cc: [email protected]
Subject: Re: Compiler switches on RISC byte alignment

Dalesio, Leo wrote:
> Is there a compiler switch on the GNU compiler to turn off the RISC byte alignment?

Define the specific structure you want with the gcc-specific syntax __attribute__((packed).  You do that like this:

struct foo {
   char a;
   short b __attribute__((packed));
   int c __attribute__((packed));
};

I think you can also put the attribute on the structure itself (after the closing {}) to pack all fields together.

Variable attributes in gcc are described here:
http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

HTH,

- Andrew
--
Dear God, I didn't think orange went with purple until I saw the sunset you made last night.  That was really cool. - Caro



Replies:
Re: Compiler switches on RISC byte alignment Lawrence T. Hoff

Navigate by Date:
Prev: Re: building base3.14.5 Andrew Johnson
Next: Re: Compiler switches on RISC byte alignment Lawrence T. Hoff
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  <20042005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
Navigate by Thread:
Prev: Re: Compiler switches on RISC byte alignment Andrew Johnson
Next: Re: Compiler switches on RISC byte alignment Lawrence T. Hoff
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  <20042005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
ANJ, 10 Aug 2010 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·