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
  • 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?

    Yes, this is exactly what the C166 compiler does. It automatically generates 2 sections ?C_CLRMEMSEC and ?C_INITSEC, and store them in the program image.

    The ?C_CLRMEMSEC keeps the info for the blocks of memory to be cleared to 0s, the ?C_INITSEC holds the info for non-zero values to be initalized for variables.

    The problem is only for big structures which have 1 or several members to be initialized as non-zero. In such case, if the structure is initialized, a lot of 0s will be saved into ?C_INITSEC which consumes ROM/FLASH space. I am thinking if it is possible to save this memory space.

    I think i will split but structures when possible. And write functions to initialize the non-zero members.

    A serious programmer really should have access to the actual standards - and, of course, a good textbook.

    I totally agree with you. But unfortunately i didn't start learning C with a good textbook. I was already thinking to rebuild my C reference source set, but the plan went slow ...

Reply
  • 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?

    Yes, this is exactly what the C166 compiler does. It automatically generates 2 sections ?C_CLRMEMSEC and ?C_INITSEC, and store them in the program image.

    The ?C_CLRMEMSEC keeps the info for the blocks of memory to be cleared to 0s, the ?C_INITSEC holds the info for non-zero values to be initalized for variables.

    The problem is only for big structures which have 1 or several members to be initialized as non-zero. In such case, if the structure is initialized, a lot of 0s will be saved into ?C_INITSEC which consumes ROM/FLASH space. I am thinking if it is possible to save this memory space.

    I think i will split but structures when possible. And write functions to initialize the non-zero members.

    A serious programmer really should have access to the actual standards - and, of course, a good textbook.

    I totally agree with you. But unfortunately i didn't start learning C with a good textbook. I was already thinking to rebuild my C reference source set, but the plan went slow ...

Children