views:

101

answers:

3

Hi, I have a question regarding free() in C.

Suppose I have a pointer to some struct (say node *ptr).. after freeing it can i Initialize it to NULL and make it point to some new location using malloc() or realloc()?

For Example:

node *ptr=NULL;
ptr=realloc(ptr,sizeof(node)); //works exactly like malloc

/* Do some operations on ptr */

free(ptr);

ptr=NULL;
ptr=realloc(ptr,sizeof(node));

Is that valid, or will it create a problem. The reason I used realloc in place of malloc is because all my realloc() calls are in a loop (so instead of sizeof(node) in the second argument it is actually n*sizeof(node) where n keeps on incrementing... and the last location in this resultant array is written with new data) where the memory pointed to by ptr keeps on increasing until the loop ends, at which point I do not require the data in the memory pointed to by ptr, so I think it best to free it. Now, all this is nested in one more bigger(outer) loop.

Thanks a lot for your help

+7  A: 

ptr doesn't remember that it was once assigned a value, and re-using it again if it was assigned NULL is no different from using it the first time around.

And since realloc() acts like malloc() when it's passed a NULL pointer, it should work just fine.

Joachim Sauer
but is it necessary to assign ptr=NULL before resusing it? I tried reusing it in a realloc() call without assigning it NULL and it gave me a segmentation fault..
assassin
and yeah... there was one instance where after assigning it NULL, and calling realloc() like ptr=(char**)realloc(ptr,(n+1)*sizeof(char*)); where n=0... it gave me a segmentation fault... What could be the possible reasons for this?
assassin
@assassin: when you don't assign NULL before reusing it, then you pass in an invalid pointer, which is definitely an error. The best idea is to assign NULL when you call `free()` and not before `realloc()` (otherwise you might assign NULL to a perfectly valid pointer and leak memory this way).
Joachim Sauer
+8  A: 

It is ok - you are not really reusing the pointer but just the variable holding the pointer.

jldupont
but is it necessary to assign ptr=NULL before resusing it? I tried reusing it in a realloc() call without assigning it NULL and it gave me a segmentation fault..
assassin
and yeah... there was one instance where after assigning it NULL, and calling realloc() like ptr=(char**)realloc(ptr,(n+1)*sizeof(char*)); where n=0... it gave me a segmentation fault... What could be the possible reasons for this?
assassin
In this case yes - you can't rely on an "old" pointer value getting in the way.
jldupont
Why don't you post more code? On what platform are you?
jldupont
could you please explain that more clearly?? Thanks
assassin
You don't want to use `realloc()` on a pointer that has been freed. `realloc()` is intended to change the size of a block of memory previously allocated.
Fred Larson
@assassin: you should use `realloc` for what it was intended: reallocating memory to an **already allocated** block.
jldupont
+3  A: 

You should not thinking about it as "freeing the pointer", but freeing whatever the pointer points to. It's perfectly normal that a pointer points first to one object (which may then be freed), and then to another object.

Hans W
but is it necessary to assign ptr=NULL before resusing it? I tried reusing it in a realloc() call without assigning it NULL and it gave me a segmentation fault..
assassin
and yeah... there was one instance where after assigning it NULL, and calling realloc() like ptr=(char**)realloc(ptr,(n+1)*sizeof(char*)); where n=0... it gave me a segmentation fault... What could be the possible reasons for this?
assassin
The reason would be that you passed a pointer to free'd memory to realloc. As you say in your code, realloc behaves like malloc when its parameter is null. This does not hold otherwise. realloc is normally used to change the size of a memory allocation. So it expects it's argument to be a pointer to a piece of allocated memory (or null). If the argument is anything else, the behvaiour is undefined which usually means segfault.
Hans W