Just a quick note. One of my friends, Roger, was on the ANSI ‘C’ committee.

One of the problems, if you have ever written an interface that either listens on a pipe for structured data or tries to read a packed binary file written by COBOL or somesuch, is that something like

1
2
3
4
5
struct record
{ char indicator[3]; 
int bill_amount 
/* etc. not sure of syntax these days */
} ;

If you try to read directly into this you will find that, even though the indicator variable is 3 bytes, the compiler will have allocated an even number or more, so that things fall easily on word boundaries. This doesn’t matter if you are writing a program that doesn’t need to talk to anything else. If you have a packed record where the fourth byte is indeed the beginning of the integer variable you are in trouble when you try and read it in.T

The traditional solution is to harass your compiler vendor until they tell you what the undocumented swiches are that make it not do word boundaries and a byte in memory is exactly a byte from your pipe or file or whatever.

In the ANSI standard, however, tucked away, you can do this:

1
2
3
4
5
6
7
8
/* assuming 4 byte int */
struct record
{ char indicator[3];
 int bill_amount;
/* etc. not sure of syntax these days */
 }
union
struct filler { char dummy[7] };

The compiler/linker is forced to byte align because the filler structure would be invalid. As I say, I’m not sure of the syntax these days but there is the idea in a nutshell. Beware that not aligning things can give you horrible errors on certain processors though.

Hope this save some pain out there.

Facebooktwittergoogle_plusredditmail