cancel
Showing results for 
Search instead for 
Did you mean: 

How does an uninitialized local pointer work in STM32?

zzzzz
Senior

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

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
Up vote any posts that you find helpful, it shows what's working..

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.