分类: LINUX
2008-07-22 22:21:47
gnu arm compiler arm-elf-gcc will treat enum as an integer type. So, please pay attention to the correct usage of it. Some compiler will treat enum as unsigned char. So if your program write like this, it will bring you the nightmare.
What’s going on?
(1) declare one enum type in headfile.
typedef enum
{
BI_SWAP_DISABLE,
BI_SWAP_ENABLE
}BI_SWAP_FLAG;
(2) define one global variable with type
u8 in c file
we had thought that the enum maybe u8 type since the
it’s member count is 2 and far less than 255.
u8 flag_bi_swap = BI_SWAP_DISABLE;
// the root cause.
u8 other_varialbe = 0;
(3 ) reference this global variable in
other.c files,
extern BI_SWAP_FLAG flag_bi_swap; // here the
compiler will treat it as integer type, 4-byte
use this extern variable
if(flag_bi_swap == BI_SWAP_ENABLE) {
do_it();
}
do_left();
After compile and link, the map file shows us that
0xa000d198
flag_fl_bbt
0xa000d199
other_varialbe
The code sometimes can running correctly,sometimes,it does not. Why???
The reason is that in other.c file, the asambly code for the red part will be
ldr r3 ,[the address of flag_bi_swap]
cmp r3,#1
bne Left
bl do_it
Left:
bl do_left
So, the issue is when the other_varialbe changes, it may influnce on the
compare conditon, and make your contion check failed.
How to fix?
Define the global variable with BI_SWAP_FLAG flag_bi_swap = BI_SWAP_DISABLE;
Simple enough, but silly at the
beginning.
Postmoterm!
We should not assume something as of course. It
works on other compiler, maybe it does not works on this compiler. Be serious
!!
Every strange thing will definitely have
the reason, we need find the root cause for it!!