1

I have a static "init" variable to run a function once on startup, (RTOS) but it seems to initialize to a random value. If I remove the static tag, everything works great. (Except for the obvious issue of it running the init function every pass.) Could anyone give more insight as to why this isn't working or perhaps a better way to achieve this?

Example code:

void ManageStructures()
{
    // Variable declarations/definitions
    static uint8_t StructInitialized;
    // Have also tried "static uint8_t StructInitialized = 0", neither worked

    // Function prototypes
    void InitStruct();

    if (!StructInitialized)
    {
        StructInitialized= 1;
        InitStruct();
    }
    Test = StructInitialized;

edit: I apologize for the lack of information. This is for a company and I am trying to stay within the bounds of our public information policy. The MCU is a STM32F7 series using the "Ac6 STM32 MCU GCC" toolchain. I am not well versed in compiler operations so it may take me longer to find answers to the compiler or makefile related questions.

edit: It has become clear that this is an issue with the compiler or linker scripts and not my code. That being said, it has also become abundantly clear that I need to learn more about toolchains, linker scripts, and compilers in general before getting to the root of this issue. I'll return to this question once I have become familiar enough to give valuable feedback or answer it myself. Thank you everyone for the feedback and direction!

19
  • 2
    That code would work in a standard environment. Your RTOS might not properly initialize the relevant data sections of your program when it starts up (e.g. forgetting to zero out the .bss section is a frequent issue when dealing with low level RTOS, or bare metal programming) leaving your static variable with whatever garbage was at that location in memory. Another issue could be the memory map of your program+rtos is not corret. Commented Mar 11, 2020 at 13:34
  • 3
    but it seems to initialize to a random value. It should be 0, otherwise your compiler is broken Commented Mar 11, 2020 at 13:35
  • 1
    @ChristianGibbons why not? Commented Mar 11, 2020 at 13:46
  • 1
    Aside the already mentioned specific compiler issues, there is another possible cause. Static variables are all stored somewhere in data segment (in BSS if not initialized or initialized to 0). If your variable is placed near an array and it is wrongly accessed (out of bounds) it might happen that your variable is someway overwritten. Commented Mar 11, 2020 at 13:47
  • 1
    ... and that is why we generally want a minimal reproducible example to refer to. No reason is evident for the code fragment presented to exhibit the behavior described, but we don't have enough information to evaluate the possibility of the program overall having UB. Commented Mar 11, 2020 at 13:50

1 Answer 1

8

It is common that embedded systems run with a "minimal startup" code, meaning that they never initialize .bss or .data during start-up. Meaning that if you write something like static int foo = 42;, the code will compile but the variable will never be set.

This isn't standard C compilant, so usually upon project creation you get an option from the IDE to have a "minimal" or "standard" startup.

This likely lies in the so-called "CRT" (C run-time) delivered with your tool chain and not in the RTOS. If you single step your program from where it actually starts (the reset vector) rather than from where main() starts, you'll be able to see exactly what the CRT does and doesn't.

Unfortunately debuggers often use a "dumbed-down mode", since embedded systems programmers are by default assumed to be completely incompetent nowadays. Meaning that they silently insert a breakpoint at main() and run until that point. You might have to "un-dumb" your debugger in order to debug the CRT.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.