Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structures & unions

Status
Not open for further replies.

stef88

MIS
Apr 27, 2007
13
GB
Hello. I have the following structures:

typedef struct CSH{
ulong H:8;
ulong S:6;
ulong C0:2;
ulong C:8;
}__attribute__((__packed__))CSH;

typedef struct CSH1{
ulong H:8;
ulong C0:2;
ulong S:6;
ulong C:8;
}__attribute__((__packed__))CSH1;

typedef struct partitie{
ulong active:8;
CSH F;
ulong sysID:8;
CSH L;
ulong start;
ulong size;
}__attribute__((__packed__))partitie;

typedef union{
partitie p;
uchar c;
}part;

part p;

I read data into p.c (16 bytes). This data has the following structure: first byte - active, second byte - FH , third byte - ( first to bits - FC0 , next 6 bits FS ), 4th byte - FC , 5 th byte sysID, bytes 6,7,8 similar to bytes 2,3,4 ; at last the next 4 bytes - start, and the last 4 bytes - size... and it works well.

However I don't quite understand why it doesn't work if I use CSH1 instead of CSH ( the logical order is H, C0, S, C not H S C0 C)

Thank you!
 
> However I don't quite understand why it doesn't work if I use CSH1 instead of CSH
As was explained in your previous post, you have NO control over the order in which bit-fields are packed into a structure.

The logical order (from your point of view) doesn't matter if the compiler writer took a different point of view.

If your data is packed [tt]HHHHHHHH C0SSSSSS CCCCCCCC[/tt], you'll have to experiment to find out which of CSH or CSH1 (or something else) actually maps to the bits you require. But I wouldn't recommend that choice of action.

As ArkM said in the other post, to be portable and guarantee an answer that works, then you need something like this.
Code:
typedef unsigned char CSH[3];

// to get S say, it would be
var.F[1] & 0x3F;  // bottom 6 bits of the 2nd byte

// to get C0 say, it would be
( var.F[1] >> 6 ) & 0x03;  // top 2 bits of 2nd byte



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Hmmm... somebody said that structures are portable, that's why I asked this :)

I know that I can do it very well on bit operations, but I thought that it's more appropriate to make it this way. Isn't there a way to make unions portable?
 
The concept of a structure, even one which contains bit-fields is portable. But that only means that every single compiler is capable of compiling the code.

What isn't portable is your attempt to force a particular interpretation on the ordering of the bits in the bit-fields.

The standard leaves open many of the issues regarding the internal detail of how various data types are stored. It is left to the compiler writer so they can make the best choice for the machine.




--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top