views:

628

answers:

9

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!

+3  A: 

Yes. That is safe. (Ugly, but safe :) )

Andrew Rollings
Read the question and tell me if it is safe " to reuse pointers after free'ing the data they point to".
Otávio Décio
Plus, in general ugly == not safe
Otávio Décio
Yes, it is safe to reassign the pointers to different blocks of memory. It is not safe to use the pointer to access the block of memory you just freed. He is reassigning the pointers, so it is safe. Read the code yourself :)
Andrew Rollings
I actually disagree that it is ugly.
BobbyShaftoe
It would probably be a bit prettier if the contents of the loop were refactored into a function.
Andrew Rollings
A: 

No.

Another tip - just because you run across a freeway without being hit doesn't make that safe either.

Which pointers do you think you are "reusing"? I don't see anything being used after it's freed, although I'm assuming that readWords and countWords allocate memory. If they don't then you have even bigger problems.

Andrew Grant
Lance Roberts
+1  A: 

I can't tell. It looks safe, but I don't see the memory allocations so I can't be sure you're freeing the right things.

Darron
+16  A: 

What you're doing is fine: because, after you release a pointer, you're reinitializing it before you reuse it.

If the question is "is it safe to reuse a pointer value after freeing it" then the answer is "no".

If the question is "is it safe to reuse a pointer variable after freeing its value" then the answer is "yes, provided you reinitialize it to a (new) valid value before you reuse it".

ChrisW
Okay. Thanks. I suppose I should reword my question to say reuse a pointer variable. I know you shouldn't use a pointer value after it has been free'd.
Nick Presta
+1  A: 

I can't see any technical issue with reusing them. It might harm from readability and maintainability perspective and increase the chance of errors.

Mehrdad Afshari
+1  A: 

It is safe to assign something else to the same pointer. It's absolutely NOT safe to re-use the free'ed memory. At one time, a lot of code would free a block of memory, and then use it as if it hadn't been free-ed. It caused massive problems when that code was ported to other operating systems that weren't so happy with that paradigm.

Paul Tomblin
it's "freed", no apostrophe. Jeez!
Thomas Tempelmann
+1  A: 

It's unclear to me what you mean by re-use.

If you mean this:

int* pInt = new int;
*pInt = 3;
delete pInt;

pInt = new int;

Then yes, it's safe.

17 of 26
This is what I mean. I'll clarify.
Nick Presta
Your point is valid, but you used a C++ program to illustrate. The question is tagged C, makes no mention of C++ and uses no C++ specific syntax. I suggest modifying your example to use C allocation functions and then deleting this comment.
Chris Young
A: 

Not clear as the allocation is not shown.

In general it is safe to reassign a pointer to point to something else after freeing what it used to point to.

Stephen Doyle
A: 

First, I fail to see a premature "freeing" here (no apostrophe!). Where do you think you access data that you've already freed, exactly?

Furthermore, even if you would access data after freeing it, it depends on the system whether this is somewhat safe or not. Systems might choose to give freed memory to other processes for re-use immediately, while other systems, especially if you use the rather private lib functions such as malloc, might just use memory that is only available to this one process of yours, so nothing will happen to freed memory because no other parts of the system will know about it.

Thomas Tempelmann