structure alignment question

  • Is there a way to force a variable to begin on a specified boundary fro C source?

    For example, if I have a structure that I want to have located on a 256-byte boundary, can the compiler automatically skip to the next 256-byte boundary before allocating space for the struct?

    Thank you.

    • Maarten Brock
      Maarten Brock

      There is only the option to store it at a specific address by using the keyword "at". The linker however will not keep that space free for this variable and might overlap it with another variable. So beware.

      The obvious question is: Why would you want to do this?

    • The reason why I want to do this is because I have a structure that has to begin on a word boundary.  Since it is located after my code, I need to make sure my code has an even number of bytes.  This is not a big problem, because if it doesn't, then I simply insert a NOP, but it would be nice not to have to go through the double compile.

      I'm looking for something like #pragma pack(n) in VC++

    • Maarten Brock
      Maarten Brock

      I still don't understand. Why do you need it to start on a word boundary? And it is a constant in code memory then?

      Anyway, VC++ pragma pack(n) does more than align the start of the struct, it aligns every element in the struct on such a boundary. This is not possible in SDCC. If you need it because of communicating through shared memory, I recommend to have the other side use pack(1).

    • It is a constant in code memory.  It has to be on at even address because I am using a SFR which automatically transfers data, and this SFR has bit 0 hardwired to 0.


    • Maarten Brock
      Maarten Brock


      You made me curious which device you're using and which sfr.

      Unfortunately SDCC cannot place constant data in code memory at a specific address (yet).