Skip to main content
zzzzz
Senior
October 14, 2021
Solved

How does an uninitialized local pointer work in STM32?

I have the following bug in my project and cannot figure out why.

In one function I was using an uninitialized pointer variable to receive data from another function. For some reason it works which I think shouldn't. The code is like this:

funcA

{

msg* pmsg;

other variables;

funcB(pmsg);

if(pmsg->memberA)

{

.....

}

if(pmsg->memberB)

{

.....

}

}

The function has been worked for a while. Does the uninitialized local variable be set to garbage value? If so, how does this thing work? Then I debug it, this pointer is always set to value 0 and I can also check the data it received. Address 0 doesn't belong to any memory section in STM32. ANybody can help? Thanks.

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    October 14, 2021

    The startup code often clears memory.

    Local/auto variable contain whatever junk is on the stack unless explicitly cleared/set.

    The zero address space is typically mapped as a shadow to FLASH, ROM or RAM per BOOTx pin selections.

    You should perhaps a) explictly clear the variable, and b) check for NULLs as you descend pointer chains.

    ie if (pmsg && pmsg->memberA) as this will drop out as if traverses left-to-write, and anything not true will break the AND test

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..

    2 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    October 14, 2021

    The startup code often clears memory.

    Local/auto variable contain whatever junk is on the stack unless explicitly cleared/set.

    The zero address space is typically mapped as a shadow to FLASH, ROM or RAM per BOOTx pin selections.

    You should perhaps a) explictly clear the variable, and b) check for NULLs as you descend pointer chains.

    ie if (pmsg && pmsg->memberA) as this will drop out as if traverses left-to-write, and anything not true will break the AND test

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    zzzzz
    zzzzzAuthor
    Senior
    October 15, 2021

    Thanks for the reply. My processor is STM32F746, It looks like the 0 address is mapped to ITCM RAM. In my code, the data is copied to that address and used immediately and no other thing messed up the data in that address. That explains why it always works. I will use a local object to replace the pointer.