EmbeddedRelated.com
Forums

data alignment question

Started by w.se...@... November 14, 2005
I am using F133 and the IAR KS.  I am trying to determine the size of a
data structure at runtime:

typedef struct _AIRFLOW_COEFFS
{
  INT8U segment;
  INT16U
    min, max;
  float offset, gain;
}_airflow_coeffs;

My addition tells me that this uses 13 bytes, but sizeof returns 14 bytes.   It
appears the compiler is word aligning the data.  Is there a way to tell the
compiler to align on byte boundaries not word?  Of course I may wrong in my
diagnosis of the problem...

Tx

/Bill

Beginning Microcontrollers with the MSP430

The pack pragma will do the trick.  Add the following to your heade
file previously to defining your structure:

#pragma pack(1)


On 11/14/05, w.sell@w.se... <w.sell@w.se...>
wrote:
>  I am using F133 and the IAR KS.  I am trying to determine the size of a
> data structure at runtime:
>
>  typedef struct _AIRFLOW_COEFFS
>  {
>    INT8U segment;
>    INT16U
>      min, max;
>    float offset, gain;
>  }_airflow_coeffs;
>
>  My addition tells me that this uses 13 bytes, but sizeof returns 14 bytes.
>  It appears the compiler is word aligning the data.  Is there a way to tell
> the compiler to align on byte boundaries not word?  Of course I may wrong
in
> my diagnosis of the problem...
>
>  Tx
>
>  /Bill
>
>
>  .
>
>
>
>
>
>  ________________________________
>  .
>
>  ________________________________
>



you need a pad byte after the 8-bit object, so the 16-bit ones are 
word-aligned - the hardware doesn't have the ability to pick up 16-bit 
objects unless they are 16-bit aligned.

You MIGHT get what you want by putting the 8-bit object(s) after, not 
before, the 16-bit ones.

David

> *Subject:* [msp430] data alignment question
> *From:* w.sell@w.se...
> *To:* msp430@msp4...
> *Date:* Mon, 14 Nov 2005 19:13:15 +0000
> 
> I am using F133 and the IAR KS.  I am trying to determine the size of 
> a data structure at runtime:
> 
> typedef struct _AIRFLOW_COEFFS
> {
>   INT8U segment;
>   INT16U
>     min, max;
>   float offset, gain;
> }_airflow_coeffs;
> 
> My addition tells me that this uses 13 bytes, but sizeof returns 14 
> bytes.   It appears the compiler is word aligning the data.  Is there 
> a way to tell the compiler to align on byte boundaries not word?  Of 
> course I may wrong in my diagnosis of the problem...
> 
> Tx
> 
> /Bill


Thanks David, but I need to keep the data structure as small as possible. 
Padding eats up more of the INFOA bank where I store this data.  If I could, I
would store this into the main flash area, but I cannot figure out a way to
protect this area from erasure each time I download a new image to flash. 
Still, someone posted the idea of using #pragma pack(1) so I will do this
instead...

later...

/Bill









> Thanks David, but I need to keep the data
structure as small as
> possible.  Padding eats up more of the INFOA bank where I store this
> data.  If I could, I would store this into the main flash area, but I
> cannot figure out a way to protect this area from erasure each time I
> download a new image to flash.  Still, someone posted the idea of
> using #pragma pack(1) so I will do this instead...

I wouldn't recommend using pragma pack(1) in this case. It is designed 
to be used when the entire data structure could be misaligned, that 
means that the compiler will generate *byte accesses* for all accesses. 
Needless to say, this will make the code much larger and slower.

A simple solution would be to use two structs, one for word-aligned data 
and one for byte-aligned.

     -- Anders Lindgren, IAR Systems
-- 
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.