Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to free local variables?

I have the following struct:

typedef struct cell Cell;
struct cell {
    int value;
    int *nextcell;
};

And I have the following function to free a linked list:

void freelist(Cell *beginning)
{
    Cell *thisCell = beginning;
    Cell *NextCell = beginning->nextcell;

   while (thisCell != NULL)
   {
        NextCell = thisCell->nextcell;
        free(thisCell);
        thisCell = NextCell;
   }

   /* Here comes my question. Do I need to free the following variables? */
   free(beginnig);
   free(thisCell);
   free(NextCell);
}
like image 741
Thi G. Avatar asked Sep 28 '13 22:09

Thi G.


People also ask

Is free necessary in C?

You should always free any memory you allocate, especially if your process may restart or keep executing. For the most part, letting the OS do it works, but it's still not really right; you should clean up your own memory.

What happens when you free a variable?

Freeing a variable deallocates its memory. While the location that variable was at may still hold the old value, it may not. It is bad practice to use a variable that has been freed, and often leads to bugs and crashes.

When should local variables be used?

Local Variable is defined as a type of variable declared within programming block or subroutines. It can only be used inside the subroutine or code block in which it is declared. The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically.

Are local variables destroyed?

Local variables are destroyed in the opposite order of creation at the end of the set of curly braces in which it is defined (or for a function parameter, at the end of the function).


2 Answers

No, freeing is intended for the dynamically allocated memory, a pointer is just a variable that points there. Your loop frees all the memory the list took - at that point there's nothing to free and trying to free the same memory again (beginning) would result in an error. thisCell after the loop is NULL there there's not even something to free there.

If you meant the pointers themselves, they did not allocate memory dynamically, when you defined them they each took a slot on the stack, and leaving the function would release that slot. Here we're only talking about the pointers themselves (the place where the address they point to is stored), not the pointed memory they might hold.

like image 173
Leeor Avatar answered Oct 12 '22 03:10

Leeor


You free memory that you allocate no matter where the pointer is stored - in a local variable, in a global / static variable, or in a pointer that is allocated on the free store itself.

Your function frees several pointers multiple times: you do not need any of the three calls to free at the bottom of your function (although the second call is harmless, because it passes NULL to free, which is always OK).

like image 27
Sergey Kalinichenko Avatar answered Oct 12 '22 03:10

Sergey Kalinichenko