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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BitField Struct Help

Status
Not open for further replies.

beefeater267

Programmer
Apr 6, 2005
79
0
0
Hi,

I'm new to C++ and am trying to use BITFIELDS. Here's the code i have thus far:

typedef unsigned int BITFIELD;

typedef struct SelfRegistrationMsg_T
{
//byte 5
BITFIELD rackID : 8;

//byte 6
BITFIELD controllerID : 8;

//byte 7 / 8
BITFIELD controllerNetmask : 8;


} SelfRegistrationMsg;


union JOTRONCmd_T
{
byte *payload;
SelfRegistrationMsg_T selfRegMsg;
};


class CJOTRON
{

public:
CJOTRON::CJOTRON();
virtual CJOTRON::~CJOTRON();

JOTRONCmd_T cmd;

bool BuildCmd(const BYTE payload[]);

private:
};


All above is in a .h file..... and in in the cooresponding .cpp file i have:


bool CJOTRON::BuildCmd(const BYTE payload[])
{
bool bReturn = false;
int payloadSize = ((sizeof(payload) / sizeof(byte)) - 1);
this->cmd.payload = new byte[payloadSize];
memcpy(&this->cmd.payload[0],&payload[0],payloadSize);
bReturn = true;
return bReturn;
}


So, i'm trying to use the BITFIELD in a test.cpp class, with code such as:

CJOTRON* jotCmd = new CJOTRON();
byte* payload;
payload = new byte[3];
memset(payload, 0, (sizeof(byte) * 3));
payload[0] = 0x02;
payload[1] = 0x03;
payload[2] = 0x04;
jotCmd->BuildCmd(payload);

unsigned int a = jotCmd->cmd.selfRegMsg.rackID;
unsigned int b = jotCmd->cmd.selfRegMsg.controllerID;
unsigned int c = jotCmd->cmd.selfRegMsg.controllerNetmask;

//**************************************
So, in my test, all i'm doing is just setting 3 values of an array and copying it into my object with BuildCmd, and just testing to read them back with my bitField struct.

however, instead of getting what i expect:
(rackID = 0x02, controllerID = 0x03, controllerNetmask = 0x04), i get back other values.

Any idea what i may be doing wrong?
 
You can't compile this code: too many syntax errors. For example, byte and BYTE and so on.
Code:
union JOTRONCmd_T        
{
        byte *payload;
        SelfRegistrationMsg_T selfRegMsg;
}
A pointer to an array (not array himself) and a structure are overlapped. So if you modify selfRegMsg then you modify a pointer too. Obviously it's not expected behaviour...

Code:
int payloadSize = ((sizeof(payload) / sizeof(byte))- 1);
1. A size of an array is sizeof(array)/sizeof(element), why -1?
2. But it's true only if sizeof operation argument is a true array, not a parameter. In your case sizeof(paiload[])is sizeof of a pointer to array!

In C and C++ array declaration w/o number of elements (parameter list only) is a pointer to the 1st element.

Alas, you present an example of a very dangerous (too intricate and complicated;) and none-portable code. For example, C and C++ languages standards do not guarantee good adjustment for your bit field structure and byte (char) array in unions...

Avoid using bit fields in C and C++: it's old fashion badly defined surrogate of bit strings...
 
ArkM,

This code compiles for me no problem. But regardless, if you think Bit fields are too old fashioned, what objects can i use to do my code?

Bottom line is that i'm writing an interface to an ICD, and i have the bit command structures, and the positions for what each byte means.

Should i use a CByteArray and just use indicies to pull out the data i want to look at?

for ex: (my syntax may be wrong, but here's some pseudocode)

CByteArray myCommand = new CByteArray();
myCommand.SetSize(3);

byte radioID = myCommand[0];
byte controllerID = myCommand[1];
byte netmaskCtrl = myCommand[2];

THanks!
 
Use unsigned char array as is (it's BYTE type alias from windef.h). The unsigned char is an integral type in C and C++; you may assign (or get) values 0xHH directly via indicies.
You may add syntax shugar to access these bytes, of course: it depends on your problem (sorry, I can't see any problem to set or get three bytes in memory).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top