Thread: #pragma pack(1) workaround?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    #pragma pack(1) workaround?

    Hey everyone, I'm new here.

    I'm writing C code for an embedded platform where I have to stream specific messages (that I do not define) in and out an interface to another device.

    The compiler I must use does not support the #pragma pack directive (or any other directive to tweak structure member alignment).

    I have (about 50) structures set up that define the messages that will be passed in and out of the interface. But when I try to lay my structures onto the byte streams, things don't work out correctly because of the padding that the compiler is inserting between structure members.

    Does anyone have any ideas about how I can convert my data to and from padded structures to un-padded byte arrays without having to write separate stream in/out functions for each structure?

    By the way, my code has to be very portable (read 100% ANSI C).

    I'd really appreciate any insight anyone has here. Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > By the way, my code has to be very portable (read 100% ANSI C).
    You've answered your own question - you have to serialise it yourself.

    Even if your current compiler had a pragma, that wouldn't help you since (as you already know), there are other compilers which don't support it.

    It's a little strange though, most compilers for embedded targets usually have a few toys to play with to make the realities of physical interfaces a little easier to deal with.

    Besides, as well as padding and alignment, there is endian as well, and there are no directives on any compiler which can fix that problem.

    > I have (about 50) structures set up that define the messages that will be passed in and out of the interface
    Perhaps http://en.wikipedia.org/wiki/Externa...Representation
    There are some tools (or perhaps even create your own) which given a struct can auto-generate the encode/decode functions.
    Certainly with that many, it would be worth looking into IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You could use offsetof to make an array of the layout of the struct. But if you have bitfields then that wouldn't work.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    3
    Thanks for the reply Salem.

    I looked into the XDR (external data representation) standard. After reading RFC 4506 it looks like for XDR encoding all members of structures land on four byte boundaries.

    Since I can't force the device I am communicating with to use XDR decoding, XDR is not an option here.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3
    My problem has been solved by a suggestion by a user on another forum. If you're interested check out the solution here: http://www.codeguru.com/forum/showth...errerid=294270

    Thanks for your suggestions!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Seems like a lot of editing of structures with macros to me.

    Personally, I would have written a perl script to parse a struct and emit all the code necessary to do the work without having to maul the source code with macro hell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. labels inside the code #pragma mark label
    By nacho4d in forum C++ Programming
    Replies: 6
    Last Post: 01-11-2009, 02:50 AM
  2. Programming a PIC 16F84 microcontroller in C
    By Iahim in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 02:23 PM
  3. what does #pragma do?
    By cbc in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2003, 06:51 AM
  4. what is a pragma comment?
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2002, 05:50 AM
  5. WORKAROUND for >> to work with __int64??
    By brian0918 in forum C++ Programming
    Replies: 7
    Last Post: 08-13-2002, 01:18 PM