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.

C++ like array initialization

Hi folks!

 

GCC allows C++ like array initialization

static unsigned char auiBuffer [100] = {0x80};

were all elements of buffer are initialized with 0x80.

Is this also possible with the TI compiler (C2000)?

Currently this code only initializes the first array element.

 

Thanks in advance.

  Guido

  • Guido,

    I don't believe the TI compilers support this. Alternate ways to initialize all elements of the array would be to use the runtime support function memset() or use a "for" loop to initialize the values.

  • The copy of g++ I use doesn't do this by default.  What version of GCC and what switches are you using to get the behavior you expect?

  • First, in case you missed it, to activate GCC support in a TI compiler you currently have to use the --gcc option.

    That said, even using --gcc option with your example the TI compiler just intiializes one element to 0x80.  Like the Archaeologist points out, gcc and g++ (for at least versions 4.1.1 and 4.4.2) do the same.

    You can initialize the whole array with one of the following:

    static unsigned char auiBuffer[100] = { [0...99] = 0x80 };

    static unsigned char auiBuffer[] = { [0...99] = 0x80 };   /* size inherited from the initializer */

  • Hi!

    Thank you all for your quick and very helpful answers.

    There was a misunderstanding on my side:

    Actually the array was initialized to zero

    static unsigned char auiBuffer [100] = {0x00};

    This code works well for GCC since it initializes
    by default all static variables with zero at startup,
    even without "= {0x00}", which only initializes the first
    element of the array.

    The TI compiler (or the coreesponding startup) does not do
    this by default and therefore all other (not initialized elements)
    show random values.

    Special thanks to Paul Knueven for his very elegant solution,
    which was new to me.

    Best regards.
      Guido

  • Guido Suedholt said:

    [GCC] initializes by default all static variables with zero at startup, even without "= {0x00}", which only initializes the first element of the array.

    The TI compiler [does not]

    You are correct, this is a known limitation of TI's implementation of COFF.  See http://processors.wiki.ti.com/index.php/TI_Compilers_and_Industry_Standards#Variations_from_the_C_standard

    Note that in EABI mode for both ARM and C6000 targets, the compiler does what you expect here.