A wierd problem when converting enumeration to unsignd chars of a structure
I am using Green Hills MULTI compilor and Integrity OS.
I recently encountered a very wierd problem: I converted an enumeration (
enum: 4 bytes) to unsigned chars (UCHAR: 1 byte) inside of function
MyTest::SetMyType (please refer to the original function 1) attached below).
After conversion, I found stMyType.ucXType was set correctly, while
stMyType.ucYType was always set to 0. Moreover, I found a different variable
ucOldMode (see definition 3), which is about 3 bytes ahead of ucYType in the
memory allocation, was set to the value which should be assigned to ucYType.
I modified the function 1) to 2), and it worked (both ucXType and ucYType
were set to the correct values, and ucOldMode was not set any more). Inside
of 2), I first created a ConfigMyEnum variable as a buffer to hold the
assigned value. I could not understand why it worked this time, anybody can
help?
Moreover, I modified the function 1) to 3), and it worked as well. I only
added a sleep function to the default clause, and according to my test, I
was very sure that the default clause was never called (I put printf
function inside of it for debug). I was confused by the fact that a
modification on an unused clause could solve the problem. Anybody can give
me a hint please?
Thanks in advance.
Johnson
//Defintion 1 -- file 1
typedef enum
{
CFG_T0 = 0,
CFG_T1 = 1,
CFG_T8 = 8,
CFG_T4 = 4
} ConfigMyEnum;
//Definition2 -- file 2
inline ConfigMyEnum GeteMyType( void ) const
{
return( eMyType );
}
ConfigMyEnum eMyType;
//Definition 3 -- file 3
struct MyOldEntry
{
UCHAR ucOldMode; //It is set to the value which should be
assigned to ucYType.
UCHAR ucCurMode;
UCHAR ucNewMode;
};
struct MyNewEntry
{
UCHAR ucYType; //Y first
UCHAR ucXType; //X second
}stMyType;
1) ---- Original function ---- Encounter Problem!
void
MyTest::SetMyType(MyTypeCmd* pclMyType_)
{
switch(pclMyType_->GeteMySystem())
{
case SYSTEM_CASE1:
stMyType.ucXType = (UCHAR) pclMyType_->GeteMyType();;
break;
case SYSTEM_CASE2:
stMyType.ucYType = (UCHAR) pclMyType_->GeteMyType();;
break;
default:
break;
}
}
2) ---- After Modifiction ------ Problem Solved!
void
MyTest::SetMyType(MyTypeCmd* pclMyType_)
{
ConfigMyEnum m_eMyType = pclMyType_->GeteMyType();
switch(pclMyType_->GeteMySystem())
{
case SYSTEM_CASE1:
stMyType.ucXType = (UCHAR) m_eMyType;
break;
case SYSTEM_CASE2:
stMyType.ucYType = (UCHAR) m_eMyType;
break;
default:
break;
}
}
3)---- After Modifiction ------ Problem Solved!
void
MyTest::SetMyType(MyTypeCmd* pclMyType_)
{
switch(pclMyType_->GeteMySystem())
{
case SYSTEM_CASE1:
stMyType.ucXType = (UCHAR) pclMyType_->GeteMyType();;
break;
case SYSTEM_CASE2:
stMyType.ucYType = (UCHAR) pclMyType_->GeteMyType();;
break;
default: //I am sure it is never called during the test.
Sleep (100); //100 ms
break;
}
}

