I have a question regarding free () in C.
Suppose I have a pointer to some structure (say node * ptr) .. after freeing it can initialize it to NULL and point to some new location using malloc () or realloc ()?
Example:
node *ptr=NULL; ptr=realloc(ptr,sizeof(node));
Is this valid or will cause a problem. The reason I used realloc instead of malloc is because all my realloc () calls are in a loop (so instead of the second argument to sizeof (node) it's n * sizeof (node)), where n continues incrementing ... and the last place in this resulting array is written with new data), where the memory pointed to by ptr continues to increase until the loop ends, and at that moment I don't need the data in the memory pointed to by ptr, so I think that it’s best to free him. Now all this is embedded in another (external) loop.
Many thanks for your help
source share