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/PRU-CGT: variable length array feature of compiler not working right

Part Number: PRU-CGT

Tool/software: TI C/C++ Compiler

Hello,

I wanted to use the variable length array feature that is supported by PRU compiler(as stated in section 5.14 of SPRUHV7C). However, when I try using it, the execution hangs.

For example, I am calling the below function from main. On debugging, the execution hangs infinitely. Any clarification is appreciated.

uint32_t compute_factorial(uint8_t number)
{
    volatile uint8_t num[number];
    uint8_t i;
    uint32_t factorial = 1;

    for (i = 0; i < number; i++)
    {
        num[i] = i + 1;
    }

    for (i = 0; i < number; i++)
    {
        factorial *= num[i];
    }

    return factorial;
}

regards

Brayan

  • The variable length array is allocated memory by calling malloc.  My guess is that malloc call failed.  When that occurs, control finally ends up in a spin loop in the RTS function abort.  Does this match what you see?  If so, try making the heap as big as possible, just to see what happens.

    Thanks and regards,

    -George

  • I don't think it matters, but why are you making the array volatile? What interrupt handler or other process do you expect to mess with an auto variable?

    Why use an array at all? Why not :

    for (i=0; i<number; i++)

    {

     factorial *= i+1;

    }

  • Did you make the heap bigger?  If so, what happened?

    Thanks and regards,

    -George

  • I agree. The code snippet is just an example to explain the problem :)
  • Hi George, I've already allocated 1Kbyte for heap. Also using malloc(as shown in snippet below) instead of VLA resolves the issue.

    uint32_t compute_factorial(uint8_t number)
    {
        uint8_t *num = malloc(number)
    
        uint8_t i;
        uint32_t factorial = 1;
    
        for (i = 0; i < number; i++)
        {
            num[i] = i + 1;
        }
    
        for (i = 0; i < number; i++)
        {
            factorial *= num[i];
        }
    
        free num;
    
        return factorial;
    }

  • "I agree. The code snippet is just an example to explain the problem :)"

    Yeah, I figured that a little while after I posted!