This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Is it possible to partially initialize a struct?

Hi, my question is to ask whether or not it is possible to partially initialize a struct in C language with Keil C166 compiler.

According to C syntax [1], it should be possible to do things like:

struct {
    int a;
    int b;
    int c;
} S = {
    .b = 0,
    .c = 1,
};

In the example above, only the selected members of the structure are given default values, the others are not.

However, the C166 compiler seems not to understand this expression. When compiling, it reports error: MAIN.C(14): error C25: syntax error near '.'

The reason why i am asking this question is because: the Keil compiler by default creates sections ?C_INITSEC and ?C_CLRMEMSEC which store data for START_V2.A66 to clear variables to zero and assign initialized values at CPU start up. So it will save (well, only a little bit) memory in the ?C_INITSEC by inilizing only non-zero varialbes or struct members only.

[1] en.wikipedia.org/.../C_syntax

Parents Reply Children
  • Isn't it at least conceivable that the compiler might first do a block fill of the entire initialised area with zeros, and store only the non-zero initialisers in its program image?

    Conceivable --- sure. But it's complex, and risks wasting more space for book-keeping information ("this byte goes to address x, the next 5 bytes go to address y, ...") than is saved by not explicitly holding all the zeroes.

    The more typical approach is to have the linker apply a data compression algorithm to the initializer section before placing it into the program image. The start-up code then decompresses that code into RAM. The classic separation into zero-initialized and copy-initialized data sections is a very simple, first step in that direction --- poor man's run-length encoding.

    But usually this is only ever done by toolchains for bigger platforms (32-bit and up), where the overhead of the decompression routine doesn't hurt as much as it would on a smaller micro.