2

From the FreeRTOS include/StackMacros.h file:

#if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )

#define taskCHECK_FOR_STACK_OVERFLOW()                                                              \
{                                                                                                   \
    const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                         \
    const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5;                                          \
                                                                                                    \
    if( ( pulStack[ 0 ] != ulCheckValue ) ||                                                \
        ( pulStack[ 1 ] != ulCheckValue ) ||                                                \
        ( pulStack[ 2 ] != ulCheckValue ) ||                                                \
        ( pulStack[ 3 ] != ulCheckValue ) )                                             \
    {                                                                                               \
        vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName );   \
    }                                                                                               \
}

#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */

If i understand it right, pxCurrentTCB is a kernel structure for running process and pxStack points stack beginning of that process. It is different every time but, somehow it can be stored in const value?

  • 2
    `pulStack` is `const` and it only lives inside the block it's declared, so why do you think this is a problem? –  Jul 13 '17 at 15:35
  • 2
    "*It is different every time but, somehow it can be stored in const value?*" <- well, in a *different* `const` object every time, so there's no contradiction. –  Jul 13 '17 at 15:40
  • 3
    `const` simply means that the object cannot be written to *after it has been initialized*; it has nothing to do with run-time vs. compile-time. – John Bode Jul 13 '17 at 15:52

2 Answers2

3

The keyword const can mean two things:

  1. If the const variable is initialized with a compile-time constant it is itself a compile-time constant.

  2. If it's initialized at run-time then it's a run-time constant, which means once initialized it can not change its value.

Using const is also a good hint for readers of the code, but could also act as a hint for the compiler which might do some optimizations that might otherwise not be possible.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Why do you draw this distinction? AFAIK, it doesn't change the semantics of `const` at all which is just the value cannot be changed for the whole lifetime of the object... –  Jul 13 '17 at 15:37
  • 2
    Obligatory constant comment of yore: [... to give names to constants; instead of referring to pi as 3.141592653589793 ... `PI` can be given that value This also simplifies modifying the program, **should the value of pi change**](https://en.wikiquote.org/wiki/Fortran). ;-) – chux - Reinstate Monica Jul 13 '17 at 16:08
0

In the definition of pulStack:

  1. The first const says that pulStack may not modify what it points to. (i.e.) pulStack[0] = ... is an error
  2. The second const says that the value within pulStack (i.e. what it points to) may not be modified within the block scope in which it was declared. (i.e.) A [subsequent] pulStack = ... is an error
Craig Estey
  • 30,627
  • 4
  • 24
  • 48