GeLLcheN wrote:
Hello everybody,
I've got a little (endian) problem.
I'm programming a network-based application, in which I'll use structs,
interpreting ethernet packets.
My intention is to read the stream directly and transport to the right
struct. But this seems to be not so simple:
Here's a sample code demonstrating my problem:
----------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
typedef unsigned short WORD;
typedef struct {
WORD address;
/*unsigned test1: 4;
unsigned test2: 4;*/
unsigned char aha;
unsigned test3: 4;
unsigned test4: 4;
} testStruct;
int main(int argc, char* argv[])
{
unsigned char aha[5]={0x12, 0x34, 0xAB, 0xCD, 0xEF};
testStruct structure;
testStruct * pStructure;
pStructure=(testStruct*)&aha;
structure=*pStructure;
return 0;
}
------------------------------------------------------------------------------------------------------------
My debugger shows:
- for word address 0x3412 correct!
- for char aha 0xAB
BUT:
- unsigned 4 test3: 0x0000000F
- unsigned 4 test4: 0x0000000E
When i use test1 and test2 (in comment brackets) it also seems to be
buggy!
It would be nice if somebody could give me a simple idea so that I
could resolve this situation.
I would be very thankful!
Regards,
JPMR
For future reference, post code that prints the output rather than
relying on the debugger. That makes it easier for us to help you. See
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8.
My guess is that you are using a Microsoft compiler and that your
packing is off. You can change the packing either with a compiler
switch or with a #pragma pack directive. This code works as I think you
want:
#include <iostream>
#include <iomanip>
using namespace std;
typedef unsigned short WORD;
#pragma pack(1)
struct testStruct {
WORD address;
unsigned test1: 4;
unsigned test2: 4;
//unsigned char aha;
unsigned test3: 4;
unsigned test4: 4;
};
int main()
{
unsigned char aha[5]={0x12, 0x34, 0xAB, 0xCD, 0xEF};
testStruct structure;
testStruct * pStructure;
pStructure=(testStruct*)&aha;
structure=*pStructure;
cout << hex << pStructure->address
// << int(pStructure->aha)
<< ' ' << pStructure->test1
<< ' ' << pStructure->test2
<< ' ' << pStructure->test3
<< ' ' << pStructure->test4
<< endl;
cout << hex << structure.address
// << ' ' << int( structure.aha)
<< ' ' << structure.test1
<< ' ' << structure.test2
<< ' ' << structure.test3
<< ' ' << structure.test4
<< endl;
return 0;
}
The output is:
3412 b a d c
3412 b a d c
Bear in mind, however, that bit-fields are non-portable. C++ARM says
that the layout of bit-fields "is highly implementation dependent, and
it is wise not to make any assumptions about it unless absolutely
necessary." Some programmers mistakenly see bit-fields as a good form
of space or speed optimization. Consult C++ARM section 9.6 and C++PL
section C.8.1 for reasons why these forms of premature optimization
often have the opposite effect. In certain scenarios, bandwidth
bottlenecks *might* be eased by bit-packing. Just remember not to
prematurely optimize (
http://www.gotw.ca/publications/mill09.htm). :-)
Cheers! --M