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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

Jump to solution
969 Views
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?

Labels (1)
0 Kudos
Reply
1 Solution
690 Views
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

View solution in original post

0 Kudos
Reply
2 Replies
691 Views
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 Kudos
Reply
690 Views
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 Kudos
Reply