-
A wierd problem when converting enumeration to unsignd chars of a structure
Hello,
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;
}
}
-
I am not sure on your exact problem, but in general: if moving code around (inserting a print, or no-op, etc) fixes/changes behavior, then *somewhere else* you very likely have a pointer out of bounds!
-
I'm afraid I lost you somewhere. In simple words: what value, of what type are you trying to convert and what is the result you're getting?
As jonnin said, the bug you encountered is most likely a pointer related bug lurking elsewhere in your code. Your code does seem to use pointers so this is quite likely. Also notice that your enums could be out of sync (this could happen when using inline functions, again, something that you want to avoid in general)
Danny Kalev
-
btw, this enum doesn't look right to me. I'm not saying that this is the source of the bug, but you certainly don't want to add more confusion:
typedef enum
{
CFG_T0 = 0,
CFG_T1 = 1,
CFG_T8 = 8,
CFG_T4 = 4
} ConfigMyEnum;
Obviously, CFG_T8 = 8 should be the last enumerator, after CFG_T4 = 4.
Danny Kalev
Similar Threads
-
By IbrahimMalluf in forum XML
Replies: 0
Last Post: 10-01-2002, 12:30 PM
-
By J Bates in forum VB Classic
Replies: 2
Last Post: 05-31-2002, 04:31 PM
-
By CHRISTOS STAVRINOU in forum VB Classic
Replies: 0
Last Post: 11-16-2000, 05:58 PM
-
By CHRISTOS STAVRINOU in forum Database
Replies: 0
Last Post: 11-16-2000, 05:56 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|