Hello all-
        I've seen quite a bit of talk about structures lately - I ran into a
problem of my own with them.  Compilers I believe have a default byte
packing boundary which aligns data members of a structure along byte
boundaries.  For example if it is set to 4 bytes, and you have the following
structure:
struct test{
        char item1;
        char item2;
        char item3;
        char item4;
        char item5;
}test_struct;
I believe if you do a sizeof(test_struct) you get 8 bytes returned because
the structure must fall on a 4 byte boundary.
        The problem lies in some libraries are created with the assumption the
compiler will default to the correct byte alignment and thus telling the
compiler at compile time what byte alignment to use is dangerous.  It is
better to use the #pragma pack() directive.  In my case, right before a
structure (or series of structures) I put in a #pragma pack(1).  At the end
of the structure(s) I put in #pragma pack() which then uses the default
compiler byte packing settings.  Thus the above structure becomes:
#pragma pack(1)
struct test{
        char item1;
        char item2;
        char item3;
        char item4;
        char item5;
}test_struct;
#pragma pack()

sizeof(test_struct) now returns 5 bytes instead of 8.  I may be wrong about
all of this as I am not a software engineer but an electrical engineer doing
double duty so don't shoot me if I'm wrong!  And if I am - please correct
me.
        This solved my problem of having a structure created in linux and being
sent across TCP/IP to a windows machine getting hashed up.  Using the pragma
pack directives on both ends fixed it immediately.

Troy Davis
Airborne Data Systems, Inc.

-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/

Reply via email to