C Not sure what to free properly

Viewed 70

I have a school homework for a linked list, this is all my functions and I need to finish the free function but I'm quite new to C so not really sure what exactly do I have to free

here is my code:

typedef struct TEmployee
{
    struct TEmployee * m_Next;
    struct TEmployee * m_Bak;
    char * m_Name;
} TEMPLOYEE;

#endif /* __PROGTEST__ */

TEMPLOYEE * newEmployee(const char * name, TEMPLOYEE * next)
{
    TEMPLOYEE* head = NULL;
    head = (TEMPLOYEE *)malloc(sizeof(TEMPLOYEE));
    if(head==NULL)
    {
        return NULL;
    }

    head -> m_Name = strdup(name);
    head -> m_Next = next;
    head -> m_Bak = NULL;

    return head;
}

int getEmpPos(TEMPLOYEE *list, TEMPLOYEE *el){
    int pos = 0;
    TEMPLOYEE *listPos = list;

    while(listPos != NULL){
        if(listPos == el)
            return pos;

        listPos = listPos->m_Next;
        pos++;
    }

    return -1;
}

TEMPLOYEE* getEmpAtPos(TEMPLOYEE* list, int pos)
{
    TEMPLOYEE *toReturn = list;
    for(int i = 0; i < pos; i++){
        toReturn = toReturn->m_Next;
    }

    return toReturn;
}

TEMPLOYEE * cloneList(TEMPLOYEE * src)
{
    TEMPLOYEE* current = src;
    TEMPLOYEE* newList = NULL;
    TEMPLOYEE* tail = NULL;

    while(current != NULL)
    {
        if(newList==NULL)
        {
            newList = (TEMPLOYEE*)malloc(sizeof(TEMPLOYEE));
            newList -> m_Name = (char *)malloc(1 + strlen(current -> m_Name));
            strcpy(newList -> m_Name, current -> m_Name);
            newList -> m_Next = NULL;
            newList -> m_Bak = NULL;
            tail = newList;
        }
        else
        {
            tail -> m_Next = (TEMPLOYEE*)malloc(sizeof(TEMPLOYEE));
            tail = tail -> m_Next;
            tail -> m_Name = (char *)malloc(1 + strlen(current -> m_Name));
            strcpy(tail -> m_Name, current -> m_Name);
            tail -> m_Next = NULL;
        }
        current = current -> m_Next;
    }

    // Clone backups
    current = src;
    tail = newList;

    while(current != NULL){
        if(current -> m_Bak)
        {
            tail -> m_Bak = getEmpAtPos(newList, getEmpPos(src, current->m_Bak));
        }
        else
        {
            tail -> m_Bak = NULL;
        }

        tail = tail -> m_Next;
        current = current -> m_Next;
    }

    return newList;
}

and this is my free function but this way it only frees some things but not others

void freeList(TEMPLOYEE * src)
{

TEMPLOYEE* tmp;

    while(src != NULL)
    {
        tmp = src;
        src = src -> m_Next;
        free(tmp);
    }
}

Any help is greatly appreciated

1 Answers

There are only two functions that allocate memory that needs to be freed.

The newEmployee function creates a string with strdup that must be freed, as well as the TEMPLOYEE object itself.

The other function that allocates memory is cloneList, however it only clones a list. If we can first figure out how to properly free one list, we only need to make sure we free all cloned lists as well to ensure we free memory allocated in cloneList.

To properly free the list, we need to free each TEMPLOYEE object and the m_Name that it holds. Your free list function can be modified like so:

void freeList(TEMPLOYEE * src)
{
    TEMPLOYEE *next = src;
    while (next != NULL) {
        TEMPLOYEE *prev = next;
        next = prev->m_Next;
        free(prev->m_Name);
        free(prev);
    }
}

As long as you call this on all lists, including those created by cloneList, all memory should be freed.

Related