Is it safe and predictable to reuse pointers after freeing the data they point to?
For example:
char* fileNames[] = { "words.txt", "moreWords.txt" };
char** words = NULL;
int* wordsCount = NULL;
for ( i = 0; i < 2; ++i ) {
data = fopen( fileNames[i], "r" );
words = readWords( data );
wordsCount = countWords( words );
free( wordsCount );
for ( j = 0; words[j]; ++j )
free( words[j] );
free( words );
fclose( data );
}
*error checking omitted
I am running the code and it appears to run (no warnings, errors, or memory problems) but I am wondering if this is safe and predictable to use in most environments (specifically in a typical Linux environment)?
If this isn't 'safe and predictable', what is the best way to accomplish the same operations on two different files, short of creating two times the amount of pointers, etc?
EDIT: I am asking if it is okay to reusing a pointer variable after freeing what it pointed to. I understand you should not use a pointer value after freeing. Assume the code works perfectly (it works as intended, memory is being freed correctly and such). I cannot change the spec. for this assignment.
Thanks!