473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

strange struct behaviour

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

Apr 5 '06 #1
3 1454
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

Apr 5 '06 #2
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

Apr 6 '06 #3
"GeLLcheN" <ge******@gmx.de> wrote in
news:11**********************@e56g2000cwe.googlegr oups.com...

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

--

Apr 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
4
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
8
by: manochavishal | last post by:
Hi, I have a structure MAX_ID is 5 /**Structure for Copies*/ typedef struct NodeVideoCopy * NodeVideoCopyPtr ; typedef struct NodeVideoCopy {
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
3
by: gorbulastic | last post by:
ok, i've created a typedef of somthing, and I put it through a for loop to put the entries into it. They are stored as an array of the typedef''s, with memory allocated via malloc command. For...
8
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.