2021-10-14 02:13 PM
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.
Solved! Go to Solution.
2021-10-14 02:37 PM
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
2021-10-14 02:37 PM
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
2021-10-15 06:59 AM
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.