0

The memory of a local array is freed once a function exits right? So why does this work when I use a block instead?

int main(int argc, char **argv){
    char *s2;

    {
        char s[] = "testing";
        s2 = s;
    }

    printf("%s\n", s2);
    return 0;
}
Koz
  • 151
  • 2
  • 14

4 Answers4

2

It is up to the implementation. The only certain thing is that s is not guaranteed to remain accessible, but of course it may as well be.

Just undefined behaviour. Hence do not rely on it.

Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
0

It actually does not exist. The results of that printf is undefined. It seems to work as expected because your specific compiler is not protecting you from doing this and the contents of that memory has not been set and changed in some other application.

The contents of the array s[] are no longer valid to be used, even by the s2 pointer.

Tim Beaudet
  • 791
  • 7
  • 16
0

Freeing a chunk of memory doesn't mean that the content written their is erased/cleared. In fact your program invokes undefined behavior.
Accessing memory chunks that were previously freed is the cause of many memory errors for novices and experienced programmers.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

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...

little_birdie
  • 5,600
  • 3
  • 23
  • 28