This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

structure byte alignment problem



Hi,

I have the following structure definition:

typedef struct packet_format
{
   uchar src;              // Packet source address
   uchar dest;             // Packet destination address
   uchar cmd;              // Command
   uchar dlen;             // Data length

   uchar data[64];   // Data buffer - must reside on even byte boundary!

} packet;

In the sample code below, "simpkt" is being allocated on an odd byte boundary.  Any help is MUCH appreciated!  thanks.

void main (void)
{
   uchar i;             // Work var
   uint  crc;           // CRC work var
   uchar ignore = true; // ADC ignore conversion flag
   packet * pkt;        // i2C packet pointer
  
#ifdef TEST
   uchar  dev_rst = false;
   volatile uchar  inp_mode = 1;
   packet simpkt;
   struct download_pt *dlpt = (struct download_pt *)&simpkt.data[0];
   struct control_pt  *ctl  = (struct control_pt  *)&simpkt.data[0];
   uchar  docnt = 0;
   uchar  doch = 1;
   uchar  aoch = 1;
   uint   aoval = 0;
   uchar  aosubtype = OUT_05;
   uchar  aosm  = 0;
   uint   ctl_tmr = 0;
#endif

.

.

.

}

 

  • Looks like you need your structure to have word alignment even though there aren't any words in it.

    One portable way of doing that is to declare another member of the structure that is a word.  Even if you have to do it with a union.

    Like this:

    typedef struct packet_format
    {
       uchar src;              // Packet source address
       uchar dest;             // Packet destination address
       uchar cmd;              // Command
       uchar dlen;             // Data length

       union {
          uchar data[64];   // Data buffer - must reside on even byte boundary!
          int alignmentKludge;
       };

    } packet;

    Now the whole structure will be aligned on an int boundary because one of the members is an int.

    Jeff

  • Thanks for the quick response Jeff.  Will give this a try first thing Monday.

    jdr

  • Jeff already pointed you to the solution. But I might add an explanation:

    jdr said:
    uchar data[64];   // Data buffer - must reside on even byte boundary

    That is the problem. A byte array is a byte array and does per definition not need a word alignment. The compiler sees that there is nothing in your struct that will ever need an alignment. That your later code may do something on the buffer that requires alignment (such as typecasting a certain offset ot an int), is actually wrong coding.
    However, I understand the problem. I ha d asimilar one when my packets arrived in a stream of packets with even and odd lengths (and paddign was not an option).

    I solved it by replacing all non-char members of the struct by byte arrays and using some macros to extract the int/long/long long values from the (unaligned) struct when necessary. The resultign binary wasn't slower or bigger than when the compiler was generating code for unaligned structs itself.

    However, when you do typecasting an (possibly odd) offset into your array to an int, you remove every chance for the compiler to know where the resulting pojtner comes from and that it could be misaligned and whatever. This may even happen if the struct itself is aligned.

  • Thanks for the info Jens.

**Attention** This is a public forum