When memory is "free" it just means that it is available to be used again. Any data that was stored there will remain until that section of memory is used again. But if you were to write more complex code, call some functions, pass some variables, allocate some variables.. you would see this memory being reused.
This is a common problem that you have to think about when you are coding in C. For example, let's say you are writing a function which returns a string value.. you need some space to store that string which will remain valid after your function exits. You cannot allocate it as a local variable within the function (or block, as you did in your question) because that memory only belongs to you for the duration of your function.
eg:
char *func1() {
mydata = "here is the data";
/* NO! */
return mydata;
}
int main(int argc, char**argv) {
char *s = func1();
/* s now contains a pointer to where mydata WAS but this memory is out of scope */
/* at some point it will be written over with something else and your program */
/* will break */
}
So, how can we write this? One common solution is:
char *func1(char *s, int len) {
mydata = "here is the data";
/* copy the data into the provided memory.. but if it is too short, */
/* make sure the string is null terminated */
strncpy(s, mydata, len);
s[len-1] = 0;
return s;
}
int main(int argc, char**argv) {
/* allocate at least as much memory as we will need */
char mymem[100];
/* pass pointer to mymem into func1, and tell func1 how much space it has */
char *s = func1(mymem, sizeof(mymem));
}
Makes sense? You can also use malloc() and free(), but that is too much for this question...