Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103128
  • 博文数量: 26
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-13 11:46
文章分类

全部博文(26)

文章存档

2011年(1)

2010年(2)

2009年(3)

2008年(20)

我的朋友
最近访客

分类: 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!!

阅读(797) | 评论(0) | 转发(0) |
0

上一篇:Linux内存管理

下一篇:System.map 格式

给主人留下些什么吧!~~