简体   繁体   中英

Static variable is not initialized to zero

I'm developing an embedded application on stm8s microcontroller using STVD IDE and COSMIC compiler.

I'm trying to define a static bool variable to use it to execute a snippet of code only once. Unexpectedly, the variable once is not initialized to FALSE or 0 . Although I explicitly initialized it. Here is the snippet of my code:

uint32_t crc32_buffer(void)
{
    static bool once = FALSE;
    uint32_t crc = 0;

    if(!once)
    {
        calcTable();
        crc = 10;
        once = TRUE;
    }

    return crc;
}

When I tried to check the physical memory location, I found that every time after entering a new debugging session (even after hardware restarting the controller) and before running the application itself, the memory address 0x80 has the same value 0x14 .

If I modified the code to assign FALSE to once after the variable's initialization:

once = FALSE;

The memory location is changed to contain 0x00 . Then if I exit this debugging session and then re-modified the code to delete this line of code and start a new debugging session, I find the memory location 0x80 has again 0x14 before running the application.

I don't understand what can prevent the compiler to initialize the variable to 0 . I don't understand what can write 0x14 to the memory location even before running the application.

I tried setting a breakpoint if the memory location 0x80 was accessed (read/write) but the application didn't stop until it reached the if statement in the code snippet.

UPDATE-2

As many pointed out the startup procedure, I don't use the default startup code. However, I'm using a custom one. When I used the standard startup code instead of the custom one I was using, the memory locations were set to 0 before main() function start execution. This is not the case with the custom startup code. So, when I define a new static variable and explicitly initialize it to FALSE , this initialization will only take place in the startup code before main() , right?

If you read this storage duration reference you will see that for static storage duration

the value stored in the object is initialized only once, prior to main function

So you have to let the startup code running before main run first. Once the main function is called the value should have been initialized.

I find the memory location 0x80 has again 0x14 before running the application.

Initialisation requires code; the state of the memory after reset, before any code has executed is non-deterministic.

I tried setting a breakpoint if the memory location 0x80 was accessed (read/write) but the application didn't stop until it reached the if statement in the code snippet.

That sounds like the initialisation right there. What value did the variable acquire at that point? Local statics can be initialised on first use, rather than in the run-time start-up. It looks to me that in fact you do not have a problem at all other than understanding the semantics of static.

If for example you place a break-point on the static declaration itself, and run it, I expect you will observe that the break point is hit once on first use initialisation, and thereafter will not break on subsequent calls to the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM