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.

Global Variables not Automatically Set to Zero.

I am writing code targeting a C28346 mcu.  I am finding that unless I explicity set a variable do 0, its initial value appears to be randon.  More specifically, If I set a watch point and start debugging, for code containing the global declarations below, the first variable is set to 0, and the second is not.

I had assumed that global data would be zeroed by the compiler.  Is there something I can do to make sure all globals get zeroed?

Int16 upSampleIndex = 0;

Int16 notInit1;

 

John Sotack

  • This is discussed in this wiki article.  Another method to consider ... Modify the boot routine _c_int00 to zero out the relevant sections (probably .bss and .ebss).

    Thanks and regards,

    -George

  • Thank you for rapid and correct response. The applicable part of the Wiki is:

    • Uninitialized static-storage-duration objects are not initialized to zero by the compiler. The user must do this explicitly in the linker. (Exceptions: ARM EABI and C6000 EABI zero-initialize all such objects.) Static storage duration objects are any global or local variables declared static. The standard says (C99 6.7.8 "Initialization", para 10) that if such objects are not explicitly initialized, they must be initialized to zero or NULL as appropriate. If an initializer for a struct or array doesn't have enough entries, the remaining uninitialized entries should be initialized as above. The TI compiler does not do this; this is left up to the user to perform in the linker command file with a fill value for sections representing initializers. This is not technically a violation of the C standard, since the linker is part of the implementation, and a method is provided for the user to get the job done, but this method is very unintuitive, and many consider it a violation. It is certainly an obstacle to ease of use.

     

    I would consider having the tool chain provide zero initialization of static scope objects by default.  This might be done by having CCS generate a link file that appropriately files .bss / ebss with zeros. 

  • Mainly C compilers do this for you. Do you have any sample code for 2803x? 

  • I'm not aware of any example code for setting all global and static variables to zero on the C28x family.

    Thanks and regards,

    -George