0

I'm developing a firmware for my STM32 board based on a STM32F412RET6 processor with STM32CubeIde and FREERTOS . When I call the va_arg function, of the stdarg.h library, for the first time get me a strange number. Why?

The code:

void readFloat(int n, ...){
    int val;
    va_list vl;
    va_start(vl,n);

    for (int i=0;i<n;i++)
    {
        val=va_arg(vl,int);
    }

    va_end(vl);
}

/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{     
    readFloat(3, 1, 2, 3);
    for(;;)
    {
        osDelay(1);
    }
    /* USER CODE END 5 */ 
}

The debug result:

enter image description here

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
jhon tonini
  • 67
  • 3
  • 7
  • Did you actually step through the relevant assignment in the debugger? – walnut Mar 20 '20 at 18:07
  • The function can be optimized out by the compiler - it does nothing. Do you compile with debugging symbols and optimizations disabled? Try adding `volatile` to `val` definition. – KamilCuk Mar 20 '20 at 18:30
  • 3
    The debugger usually highlights the *next* line that will run. So that line hasn't run yet and val is uninitialized! – user253751 Mar 20 '20 at 18:33
  • @KamilCuk : Why!? – Clifford Mar 21 '20 at 15:02
  • Try reading `val` _after_ it has been assigned I suggest. The "strange number" is the non-deterministic value of an unitialised variable. Your break-point is _before_ the call and assignment. Declare `val` with initialisation to demonstrate that, e.g.: `int val = 0 ;` The strange number is in fact 0xA5A5A5A5 which looks like stack filler used to detect the high-tide mark of stack usage. – Clifford Mar 21 '20 at 15:04

0 Answers0