Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reassigning to a pointer variable after freeing it [duplicate]

Is this legal to do? Can you assign ptr to something after it has been freed?

int * ptr = (int*) malloc(sizeof(int));
free(ptr);
ptr = (int *) malloc(sizeof(int));
like image 718
skmiley1 Avatar asked Aug 22 '17 06:08

skmiley1


People also ask

What happens to a pointer when it is freed?

Calling free() on a null pointer results in no action being taken by free() . Setting message to NULL after it is freed eliminates the possibility that the message pointer can be used to free the same memory more than once.

Can I use a pointer after freeing it?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address.

What happens if you free a pointer twice in C?

If we free the same pointer two or more time, then the behavior is undefined. So, if we free the same pointer which is freed already, the program will stop its execution.

What happens when a pointer is released twice?

The pointer still points to that memory address. At this point that same space in the heap can be returned by another malloc call. When you invoke free a second time, you are not freeing the previous data, but the new data, and this may not be good for your program ;) Thank you for the explanation!


Video Answer


4 Answers

You aren't reassigning the pointer after freeing it, you're just reusing the ptr variable. This is perfectly fine.

like image 179
Mureinik Avatar answered Oct 25 '22 08:10

Mureinik


First of all, as I mentioned in the comments, please see this discussion on why not to cast the return value of malloc() and family in C..

That said, yes, the (re)-assignment is fine here.

Actually, the question wording

Can you assign ptr to something after it has been freed?

should read

Can you assign ptr with something after it has been freed?

Guess what, the assignment without free-ing is also legal as per C, but it will create a memory leak as a side effect as the variable ptr was holding the return value of a memory allocator function and you need to free the memory once you're done using it. If, you re-assign the pointer without keeping a copy of the pointer, you'll lose the access to the memory allocated by allocator function and will have no ways to free() it.

In case the pointer was holding an address of statically allocated variable, you don't get to (nned to) free it and direct re-assignment is perfectly fine. think of this below snippet.

int x = 5, y = 10;
int * p = &x;
//do something with p
p = &y; //this is fine, just a re-assignment.
like image 7
Sourav Ghosh Avatar answered Oct 25 '22 08:10

Sourav Ghosh


Yes. it is a valid in C language.

Related stack overflow question for more information: Reusing freed pointers in C

According to cwe.mitre.org:

In this scenario, the memory in question is allocated to another pointer validly at some point after it has been freed. The original pointer to the freed memory is used again and points to somewhere within the new allocation. As the data is changed, it corrupts the validly used memory; this induces undefined behavior in the process.

like image 6
msc Avatar answered Oct 25 '22 08:10

msc


Can you assign ptr to something after it has been freed?

int * ptr = (int*) malloc(sizeof(int)); /* line 1 */
free(ptr); /* line 2 */
ptr = (int *) malloc(sizeof(int)); /* line 3 */

Taking your question as:

"Is it legal to assign the address of freshly, dynamically allocated memory to a pointer (line 3), after the memory this pointer pointed to from a previous dynamical allocation (line 1) had been freed (line2)?"

Then this answer is yes.


Running line 3 would also be valid without having run line 2. Still, if not calling free() (line 2), the value assigned to ptr (line 1) is overwritten (line 3), and with this the possibility to call free() on ptr's initial value is lost, which in turn leaves the program with leaking exactly this memory allocated initially.

like image 5
alk Avatar answered Oct 25 '22 10:10

alk