Nick Presta
Nick Presta

Reputation: 28665

Is it safe to reuse pointers variables after freeing what they point to?

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!

Upvotes: 11

Views: 8055

Answers (9)

Thomas Tempelmann
Thomas Tempelmann

Reputation: 12043

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.

Upvotes: 0

Stephen Doyle
Stephen Doyle

Reputation: 3744

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.

Upvotes: 0

17 of 26
17 of 26

Reputation: 27382

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.

Upvotes: 1

Paul Tomblin
Paul Tomblin

Reputation: 182772

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.

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

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

Upvotes: 1

ChrisW
ChrisW

Reputation: 56113

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".

Upvotes: 22

Darron
Darron

Reputation: 21628

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.

Upvotes: 1

Andrew Grant
Andrew Grant

Reputation: 58786

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.

Upvotes: 0

Andrew Rollings
Andrew Rollings

Reputation: 14551

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

Upvotes: 3

Related Questions