Fritz
Fritz

Reputation: 943

global scoped variables in c++ not properly initialized

I ported my code from stm32f072 to stm32f105. Since then I have an issue with global variables, it seems they don't get initialized. I use Atollic TrueStudio and their compile and build tools. For the new platform I tried converting the existing "eclipse" project and also to create a new one from CubeMx.

I got a Hardfault when accessing a global object. The "workaround" was moving the new statement in the accessing function. The debugger showed that even when having the new statement the global var was 0x00.

I also use an std::queue which has an size() of 0xffffffe7 when not inserted anything yet, which let me believe this also comes from a missing initialization.

I want to solve the issue, not move all init in the beginning of the main function as a workaround.

My code looks something like this:

#include <queue>

std::queue<Telegram> telegram_in_fifo;
Port *port1 = new IoPort(/* some args */);

void some_function() { // tried calling from Interrupt or from main 
    // port1 is 0x00 here
    port1 = new IoPort(/* some args */);
    // now port1 has proper address and accessing internal data works without hardfaults

    uint64_t size = telegram_in_fifo.size(); // this is 0xffffffe7
    if(size <= fifo_max) {
        telegram_in_fifo.push(telegram);
    }
}

Upvotes: 0

Views: 175

Answers (0)

Related Questions