This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to initialize static variables at compile time?



Hi,

When I compile my code, static variables are not initialized to 0!

How can I fix this?

I am using CCE v3.1 Build: 3.2.3.6.4.

  • Thanks,

    but it does not work!

    My link command file looks like this:

    SECTIONS
    {
        .bss             : {} > RAM                      /* GLOBAL & STATIC VARS              */
        .sysmem    : {} > RAM                      /* DYNAMIC MEMORY ALLOCATION AREA    */
        .stack          : {} > RAM (HIGH)         /* SOFTWARE SYSTEM STACK             */

        .text              : {}>> FLASH | FLASH2     /* CODE                              */

    ...

    }

    If I change to

    .bss: {} = 0x00;

    the linking fails.

    .bss: {} = 0x00

    .bss: {} > RAM

    also fails.

    So can you please be more detailed in your answer?

     

  • Ok,

    this is the way to do it:

    .bss : {} > RAM, fill = 0x00

     

    But, this will only initialize at load time, not at reset!

    So, the only way is to fix it in the code. This is very bad.

    I consider this a bug in CCE.

  • Do you see this behavior after a hard reset or soft reset (reset from the IDE)? I would think a hard reset would cause the on-chip bootloader to run again, thus re-filling the .bss table with your value. A soft-reset is not a true reset in the sense that it basically moves the Program Counter and that's it.

  • I'm working with a RTSC project in CCSv4 and see teh same issue with the .bss not being initialized. I can manually edit the linker.cmd to zero fill the .bss section, but it will be overwritten the next time the RTSC project is modified. What is the best way to keep the zeroing the .bss section in this case?

    Thanks

    David Kelly

  • Hi PeterETT,

    what is your code looking like? Is it like:

    static int counter; ?

    This would be according to ANSI/ISO C standard and (some) compilers pre-initialize them to 0.

    However, the TI CCE does not pre-initialize variables and it is up to your application  to do it. So change the above code line to:

    static int counter = 0;

    and you're done.

    Rgds
    aBUGSworstnightmare 

  • Hi aBUGSworstnightmare,

    In my application, I use the QP framework which relies on static variables being initialized to 0 by the compiler. As you suspected, they are declared like this:

    static int counter;

     

    It would be a massive effort to change all instances of this kind of declaration in the QP code, so I was hoping for a quicker solution.

  • Hi David,

    I solved it by manually zero the .bss as the first thing I do in the main() function:

    static uint8_t foo = 0xde;
    static uint8_t bar;

    void main( void ) {
     uint16_t p;
     for( p = 0x200; p < 0x2ec; p++ ) {
      *((uint8_t*)p) = 0;
     
     }

    Beware, any static variable that was initialized to something else ( foo in the above example ) will be set to zero also.

    /Peter