Initialization of arrays

cancel
Showing results for 
Search instead for 
Did you mean: 

Initialization of arrays

446 Views
spacedog
Contributor I

I'm using CodeWarrior 10.6 for the MC56f82748 (but the question applies to anything using this compiler). 

 

In the manual it says: "Initialize local arrays and structures at declaration time, if possible. Local arrays and structures are initialized optimally by the compiler." (Section 11.5.10.3.2 Miscellaneous Techniques, in CodeWarrior Development Studio for Microcontrollers V10.x Digital Signal Controller Build Tools Reference Manual, Rev. 10.6, 02/2014). 

 

What does that mean, exactly? 

 

Or more specifically if I have an structure like: 

 

typedef struct {
   unsigned current_size;
   unsigned items[0x64];
} q_t;

volatile q_t q = {0x01, 0x01};

Will the compiler generate 202 bytes of data to initialize this (with 198 of them being zero)? Or will it generate some sort of compressed initialization code / stored data? 

 

How can I see what data is generated for the initialization and how it is used to initialize the variables. Is there any way I can control the behaviour? 

Labels (1)
0 Kudos
2 Replies

258 Views
TICS_Fiona
NXP Employee
NXP Employee

Hello  Colin

The DSC compiler only generates data for initialized value that is not zero.   This initialized value is usually saved in data flash. To know where it is allocated, you can open S-record file and search it after building the project.

I do not think that user needs to control this behavior, because compiler does not reserve memory for the initialized value 0.

In the manual it says: "Initialize local arrays and structures at declaration time, if possible. Local arrays and structures are initialized optimally by the compiler." This means that if user initializes the local arrays and structures in the execution time, not at declaration time, the performance will be lower.


Best Regards
Fiona Kuang
TIC - Technical Information Center
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Mark Correct button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

258 Views
spacedog
Contributor I

Hi Fiona, 

I do not think that user needs to control this behavior, because compiler does not reserve memory for the initialized value 0.

In the S record I can very clearly see the zeros for the structure initialization. So they will take up space in flash. Similarly if I increase the size of the structure in my example the S record gets bigger. 

The DSC compiler only generates data for initialized value that is not zero.   This initialized value is usually saved in data flash. To know where it is allocated, you can open S-record file and search it after building the project.

This is true if an entire variable is zero. However I want it to be true when part of a variable is zero, i.e. when a structure has a few initialized elements and then a block of zeros.

Other compilers compress this data, i.e.: ARM Compiler armlink User Guide Version 5.06 | Optimization with RW data compression – ARM Developer 

But I think, for the example above, the zero values *will* be stored in flash by the CodeWarrior toolchain  -- I'm looking for formal confirmation of this if there are options to change the behavior (i.e. enable compression). 

I understand that it's unlikely to be added if not already present. 

0 Kudos