我有以下struct
:我是否需要释放局部变量?
typedef struct cell Cell;
struct cell {
int value;
int *nextcell;
};
而且我有以下函数来释放一个链表:
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);
}
你不释放变量,你释放内存。经验法则:每个malloc(或者realloc)一个免费。 –