Because of the holidays, TI E2E™ design support forum responses will be delayed from Dec. 25 through Jan. 2. Thank you for your patience.

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.

Compiler/TMS320F28377S: Problem at Array size initialization

Part Number: TMS320F28377S

Tool/software: TI C/C++ Compiler

Hi,

I have a problem with array size assignment, If I define an array variable and assign its size as a constant integer, the code is running as it is expected. However, If I assign the size with variable name, the code does not work.

For example;

int a = 2;

float b[a];

the code snippet that is given above does not work (code stop running or stuck at there) (I also try using different hardware like Arduino, there seems no problem with the same code)

When I change the code like,

float b[2]; (deleting variable 'a')

the code is running properly.

Is there anyone who faced with this kind of problem?

  • Assuming that these are globals, this is expected C behaviour. You have at least two choices:

    Make the variable a const:

    const int a = 2;

    (I am not entirely sure this will work)

    The preferred solution is to use a #define:

    #define a  (2)

  • Hi,

    They are not global variables, I also tried 'const'  but, it did not work.

    In fact, I can not use macro(#define a (2)), In my algorithm, the size of the array needs to be changed or stay constant in every iteration.

    This is why I need to use a variable to define the size of the array. Lastly, If it is C behavior, then why it does not happen in Arduino?

  • user5162189 said:
    This is why I need to use a variable to define the size of the array.

    The TI compiler allocates VLA (variable-length-arrays) using the heap. If there is insufficient heap space the allocation fails. See Compiler/TMS320F28379D: Compiler support for variable length arrays

  • Hi,

    In the thread that you gave, the array size is determined via function call, is it the same problem with mine?

    In my problem, I am not calling a function to initialize array size.

    Also, I changed my code as;

    int a = 1;

    float b[a];

    it also does not work. I assume that I have no space for heap since a = 1, is the value of 'a' important? I mean, if I change the value of 'a' as 1000,

    is there any difference?

    Lastly, how can I increase the heap size(changing the .cmd?), and depending on what?

  • Using even one instance of VLA like this ...

    user5162189 said:

    int a = 1;

    float b[a];

    ... requires a large amount of heap memory.  For an idea of how much heap is required, please see this forum thread.  Note the request to reduce the amount of heap required.  

    user5162189 said:
    how can I increase the heap size(changing the .cmd?), and depending on what?

    By using the linker option  --heap_size=number.  I presume your code is organized as a Code Composer Studio project.  The screen shot below shows how to change that setting.

    Thanks and regards,

    -George

  • Hi,

    Thank you guys for your advice, As you said, the problem is resolved when I increase the heap size.