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.

Compile/Link time initialization of union

Other Parts Discussed in Thread: TMS320F28335

I have a structure that fits the following layout:

typdef union
{
   BITFIELD_DATA_TYPE bits; //breaks out to 32 bits
   uint32_t word;
} UNION_DATA_TYPE;

typedef struct
{
   UNION_DATA_TYPE A;
   UNION_DATA_TYPE B;
   UNION_DATA_TYPE C;
   UNION_DATA_TYPE D;
} DATA_BLOCK_TYPE;

At the global scope in a code file, the variable is then declared:

DATA_BLOCK_TYPE GlobalBlock;

I would like to set the initial values of "GlobalBlock" without having to call a function to set this up, since I always expect a specific set of initial values.

I tried at first something like:

DATA_BLOCK_TYPE GlobalBlock = {0x1000, 0x10000, 0x100000, 0x1000000};

However, this resulted in a warning "#70-D integer conversion resulted in truncation".

I converted the values to 0x...L (e.g.: 0x1000L) and then 0x...UL (e.g.: 0x1000UL), but this did not resolve anything. The code would compile; however, the variable was not initialized.

I wrapped the initializer in extra brackets, due to sub members:

DATA_BLOCK_TYPE GlobalBlock = {{0x1000}, {0x10000}, {0x100000}, {0x1000000}};

However, this did not make a difference either.

Doing some searching around, I saw a number of suggestions for initializing by including the member name:

DATA_BLOCK_TYPE GlobalBlock = {{.A = 0}, ...};

When trying this, the warning was converted to an error "#29 expected an expression".

Is there any easy way that I am overlooking to get this initial value set at compile/link time?

  • Hi,

    Could depend on the exact compiler version you are using, but since in C only the first union field can be initialized, you can try to simple change the order of the field so to be able to initialize the "word" view of your field:

    typdef union {
      uint32_t word;
      BITFIELD_DATA_TYPE bits; //breaks out to 32 bits
    } UNION_DATA_TYPE;

    Or, if supported by your compiler, enable the GCC extension (see in the language settings) and use the explicit initialization:

    DATA_BLOCK_TYPE GlobalBlock = {.A.word = 0, ...};

    or:

    DATA_BLOCK_TYPE GlobalBlock = {{.word== 0}, ...};
    

    (edited: sorry, fix code formatting)

  • The compiler in use is CL2000 for TMS320F28335 version 6.1.0.

    I can't change the compile flags/pull in non-standard extensions.

    I'll take a look at flipping the order of the member declarations in the union.

    If that can't be touched for various reasons, is there a way to initialize the bit field directly without using explicit initialization?

  • Given these limitations ...

    Jamie McPeek said:
    I can't change the compile flags/pull in non-standard extensions.

    this ...

    Jamie McPeek said:
    I'll take a look at flipping the order of the member declarations in the union.

    is the only method left.

    Thanks and regards,

    -George

  • Jamie McPeek said:

    The compiler in use is CL2000 for TMS320F28335 version 6.1.0.

    I can't change the compile flags/pull in non-standard extensions.

    I'll take a look at flipping the order of the member declarations in the union.

    If that can't be touched for various reasons, is there a way to initialize the bit field directly without using explicit initialization?

    In this case, I suppose the only option is a tedious field-per-field initialization list: The compiler should merge it in only one word, compilation time calculated initialization value:

    GlobalBlock =
    {
      { first_bitfiled_value, second_bitfield_value, ....},  //A
      { ....}, //B
      ...
    };
    

    You can use a macro to build the initialization list, but this works only if all the fields are 1 btis size.

  • Flipping the order of the members in the union resolved the compiler warning and addressed the issue of it not getting initialized.

    I have marked Ablerto's response as the answer to this question.