I have a struct:
typedef struct
{
int a;
char str[9];
} myStruct;
I have a global variable of this struct:
myStruct globalStruct;
Then I have a function that is called pretty regularly and is the only real way the globalStruct
gets changed:
static void myFunc()
{
myStruct tmp;
setStruct(&tmp);
globalStruct = tmp;
return;
}
The setStruct
sometimes changes both fields in the struct and sometimes it only changes myStruct.a
. The issue I am seeing is that even though tmp
is declared on what I thought would be the stack it is actually keeping the value from the previous function call and if I don't set myStruct.str
it will just put the previous value in globalStruct
. I understand this problem can be solved by just doing memset(tmp, 0x0, sizeof(tmp))
my question is more why is this happening to begin with? I thought the memory in tmp
would have been freed after the end of the function.