The constant variable has been changed even before the program starts!

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

The constant variable has been changed even before the program starts!

跳至解决方案
784 次查看
GangHan
Contributor I

I never suffer such a weird thing.

 

Platform: MPC5668GKIT

IDE: CodeWarrior_MCU_10.2

 

In my source code, a constant array variable is defined as a initialized value of 0. Then, I compile it and download it to the RAM of MPC5668G in debug mode with the CodeWarrior debugger. And the code is set to stop at the entry point.

However, when I read the const variable, it turns out to be 0x 40009338!

 

Tom said that:

  • How does CodeWarrior initialise the DATA area? Some systems have a fixed slab of data, and others compress the data area, and run some code to fill it (or fill parts with repeated values). CW might be doing that. You should be able to find some documentation on that. Put a breakpoint further along and see if the data "comes good".

 

However, I just use the Compilor and debugger of CodeWarrior.  All my code is contained in another project (Eclipse of an RTOS Erika). I don't think it could uncompress the .elf file and fill in the data. Actually, even if the program stops at main(), the data in memory still don't match that in source code.

 

Besides, I checked the memory of target MCU and .elf file. The data in sbss2 and  sbss sections in memory do not match those in the source code, and these data take up no space in .elf file.

 

I know that all data in sbss2 and sbss are initialized to 0 or un-initialized, so it is reasonable to allocate no space for them in .elf file. And consequently, when downloaded into memory, the debuger should initialized all these data as 0

 

But I checked the corresponding address in memory, some are initialized to 0 while some are not.

why aren't they all initialized?

标签 (1)
标记 (1)
0 项奖励
1 解答
505 次查看
CrasyCat
Specialist III

Hello

 

  Per default the compiler treats data or constants initialized with a value of 0 as uninitialized variables.

  So the variables get allocated in section bss or sbss (depending on their size).

 

  If you want the compiler to place zero-initialized data into the initialized data section instead of the BSS section, you

  need to specify following pragma before the variable definition.

       

      #pragma explicit_zero_data on

 

  You can go back to default behavior using following line:

 

     #pragma explicit_zero_data off

 

CrasyCat

在原帖中查看解决方案

0 项奖励
2 回复数
506 次查看
CrasyCat
Specialist III

Hello

 

  Per default the compiler treats data or constants initialized with a value of 0 as uninitialized variables.

  So the variables get allocated in section bss or sbss (depending on their size).

 

  If you want the compiler to place zero-initialized data into the initialized data section instead of the BSS section, you

  need to specify following pragma before the variable definition.

       

      #pragma explicit_zero_data on

 

  You can go back to default behavior using following line:

 

     #pragma explicit_zero_data off

 

CrasyCat

0 项奖励
505 次查看
GangHan
Contributor I

Hi CrasyCat,

 

Thank you for your solution.

I found that the bootstrap of my RTOS just initialized .sbss and .bss, rather than .sbss2. So I'll try your suggestion.

0 项奖励