-10

free(variable);, then I really need to use variable's value once more. Will it work? Is it a bad practice?

good_evening
  • 21,085
  • 65
  • 193
  • 298

5 Answers5

11

Will it work?

IDK. It's undefined behavior, so it might or might not work.

Is it a bad practice?

Yes.

7

It will work just fine!

int *f = malloc( 128 );
free( f );
printf( "F has the value: %p\n", f );

Now, if you want the value that was in *f, that's another question.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 10
    This is the least helpful technically correct answer ever! – Pete Fordham Dec 11 '12 at 20:40
  • @PeteFordham http://theinfosphere.org/images/thumb/9/9f/Number_1.0.png/180px-Number_1.0.png – asawyer Dec 11 '12 at 20:48
  • 3
    Please.. for the sake of all those who fail to understand how pointers and memory management work... add a big fat warning to this answer. I don't want anyone using it as a reference on why what he's doing is acceptable. – ThiefMaster Dec 12 '12 at 00:34
  • 1
    How is this technically correct? o.O Using a pointer in any way after freeing it is undefined behavior. –  Dec 12 '12 at 05:52
  • 1
    @H2CO3: *Dereferencing* such a pointer in any way is UB. Printing it, though? Its pointee has been freed, but the pointer itself still has a value -- that value just isn't useful anymore. – cHao Dec 13 '12 at 15:16
  • 1
    @cHao Logically and actually it is the case, of course, but the C standard says that using it in **any way** (including getting the address, even without dereferencing it, and performing pointer arithmetic) is undefined. –  Dec 13 '12 at 15:38
  • This is undefined behavior, but does not fall under 'not an answer'. Please hold your flags folks. – Tim Post Jan 04 '13 at 14:59
4

See the man page for free():

The free() function frees the memory space pointed to by ptr,

That means when you call malloc() you own the memory that it's pointing to, you know exactly what is there, once you call:

free(variable);

It is no longer owned by you. All bets are off. There is no good reason to access memory once it's been freed. If you really need it, you can not free it. If you must free it, make a copy of the data (if that's what you need)

Mike
  • 47,263
  • 29
  • 113
  • 177
3

The variable is still available for use; the heap memory that it pointed to is not.

IOW, this is perfectly reasonable (if contrived):

int *variable = malloc(sizeof *variable);
*variable = 1;
printf("%p: %d", (void *) variable, *variable);
free(variable);
variable = malloc(sizeof *variable);
*variable = 2;
printf("%p: %d", (void *) variable, *variable);

After calling free, I can assign a new pointer value to variable and continue to work with it. In that sense, I can continue to use variable.

This, however, will not work (or at least, its behavior is undefined):

int *variable = malloc(sizeof *variable);
*variable = 1;
free(variable);
*variable = 2;

After calling free, the memory that variable used to point to may not be available anymore. Attempting to write to it could lead to a crash, or corrupt memory you're using elsewhere, or it could appear to work just fine.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Once freed, you should not reuse the variable. If you need it again, move your freeing of the variable until after you will ever need it.

Freeing a variable deallocates its memory. While the location that variable was at may still hold the old value, it may not. It is bad practice to use a variable that has been freed, and often leads to bugs and crashes.

StarPilot
  • 2,246
  • 1
  • 16
  • 18
  • 1
    Bonus fun: On linux, `free` may actually [unmap](http://www.kernel.org/doc/man-pages/online/pages/man2/mmap.2.html) the memory area so accessing it afterwards causes a segfault, not just (possibly corrupted) data. – melpomene Dec 11 '12 at 20:08