Friend asked on AIM:
I’m curious about how static variables work. Presumably they aren’t stored in the stack, right? So how come you don’t have to store it as a pointer?
They’re just global variables, but with limited visibility. So their actual storage is off with all the other global variables, but nobody outside of that function can actually see it. like having a spy in their midst.
Where is that? I guess my confusion is that it seemed like the sort of thing you’d have to allocate memory for.
Have an illustration:
so the arrows show where stuff lives / ends up. arg
and the local vars are on the stack.
The four bytes for the ‘blah
‘ pointer is on the stack. The return from malloc
is in the heap, and the static
local is off hanging out with the globals.
I thought char * blah and static char blah were basically the same thing
char * blah
has a short life span. Kind of like a firefly.
static char *blah
has a long life span. Kind of like a roach that’ll never leave the kitchen. attacks with Raid™ notwithstanding.
Definitely a kill -9 situation then.
So with char * blah if you forget to free the memory before the function exits you can’t get to it again, right? Because the pointer to it is gone
if you do char *blah = malloc (23);
and then exit the function before free()
ing, then yes, it’s gone. It’s occupying space, but there’s no map to find it
So that’s a leak
Yep!
What I find so ineentstirg is you could never find this anywhere else.
Comment by Spike — March 23, 2017 @ 11:53 am