strange struct behaviour

G

GeLLcheN

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
 
M

mlimber

GeLLcheN said:
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-lite/how-to-post.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
 
G

GeLLcheN

Thank you very much for your advices.
I tried to implement your modifications, but as I declare "aha" in the
struct like this:

typedef unsigned short WORD;
typedef unsigned char U_CHAR;

#pragma pack(1)


struct testStruct {
WORD address;
unsigned test1: 4;
unsigned test2: 4;
U_CHAR aha;
unsigned test3: 4;
unsigned test4: 4;


};

And output the result with this:
cout << hex << structure.address
<< ' ' << structure.test1
<< ' ' << structure.test2
<< ' ' << int(structure.aha)
<< ' ' << structure.test3
<< ' ' << structure.test4
<< endl;

The output is:
3412 b a cc c c

.....

Strange...

Any more ideas???

Best regards and thanks a lot
 
K

Kasimir

The output is:
3412 b a cc c c

....

Strange...

Any more ideas???

Hi,

it's a buffer-overflow-error: your initialization-buffer aha is too
small.
To ensure this, include
assert( sizeof(aha) == sizeof(testStruct) );
into your main-function.

You have to declare testStruct as follows:

struct testStruct {
WORD address;
U_CHAR test1: 4;
U_CHAR test2: 4;
U_CHAR aha;
U_CHAR test3: 4;
U_CHAR test4: 4;
};


btw: you shouldn't forget #pragma(push) and #pragma(pop) to restore
your compiler-settings

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top