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

- 21,085
- 65
- 193
- 298
-
12Have you considered not `free`ing it until your done with it? – asawyer Dec 11 '12 at 20:02
-
6Don't let go of something until you're *really* ready to let it go... – Makoto Dec 11 '12 at 20:02
-
2If you _really_ need it, it won't work. If you **really really** need it, then it might work. – Daniel Kamil Kozar Dec 11 '12 at 20:04
-
1@DanielKamilKozar http://en.wikipedia.org/wiki/When_You_Wish_upon_a_Star – asawyer Dec 11 '12 at 20:05
-
1@Bye Please read this answer: http://stackoverflow.com/a/6445794/426894 – asawyer Dec 11 '12 at 20:07
-
1"Location : Php world" pretty much explains everything about this question. – Daniel Kamil Kozar Dec 11 '12 at 20:07
-
@DanielKamilKozar: I dunno. In PHP, once you lose your references to stuff, it's pretty much *gone*. He wouldn't have gotten these harebrained ideas about ghost values from there. This sounds more like wishful thinking. – cHao Dec 11 '12 at 20:11
5 Answers
Will it work?
IDK. It's undefined behavior, so it might or might not work.
Is it a bad practice?
Yes.
-
-
6
-
3@bye you need to redesign your application so that you don't need it after freeing it. – udoprog Dec 11 '12 at 20:05
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.

- 204,365
- 48
- 270
- 300
-
10
-
@PeteFordham http://theinfosphere.org/images/thumb/9/9f/Number_1.0.png/180px-Number_1.0.png – asawyer Dec 11 '12 at 20:48
-
3Please.. 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
-
1How 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
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)

- 47,263
- 29
- 113
- 177
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.

- 119,563
- 19
- 122
- 198
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.

- 2,246
- 1
- 16
- 18
-
1Bonus 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