Search code examples
clinked-listfreeargv

Why is allocated memory not getting freed


I'm having a problem and I don't really know why and how it happens. I'll try to explain myself as much as I can.

The code has two functions: the main and assemble. In main I just grab some arguments which are document file directions, and I send each one to the assemble function.

In the assemble function I open the file, then I start a linked list, I locate memory for the first node, then I add a lot of nodes to the list (that part I didn't copy it here because it's huge but it works fine) and then at the end I close the file and I free the linked list.

This is the code:

typedef struct node {
    int location;
    char name[MAX_LABEL];
    char type;
    struct node * next;
} labVar;

int main(int argc, char *argv[]) {
if (argc == 1)
    printf("No file attached./n");
else {
    while(--argc) {
        argv++;
        assemble(*argv);
    }
}
return 0;
}

void assemble(char *file) {

FILE *iofp;
int i = 0;
labVar *nodeLabHead = NULL; /* initializing head of the labels' linked list */
labVar *nodeLabCurr = NULL; /* variable to go through the list */

if ((iofp=fopen(file,"r")) == NULL)
    printf("\nCan't open file \"%s\"\n", file);
else {
    printf("\nSuccess opening %s\n", file);

    nodeLabHead = (labVar *) malloc(sizeof(labVar));
    if (nodeLabHead == NULL) {
        printf("\nNo memory to allocate.\n");
        exit(0);
    }
    
    /*HERE I FILL  THE LINKED LIST WITH MANY NODES IN ANOTHER FUNCTION, IT WORKS*/
    
    fclose(iofp);

    while (nodeLabHead->next != NULL) {
        nodeLabCurr = nodeLabHead;
        nodeLabHead = nodeLabHead->next;
        free(nodeLabCurr);
    }
    free(nodeLabHead);
}
}

So, the code seems to be working perfectly fine with all kinds of files I've send as an argument. But when I try to run the code with more than one argument (calling the append function for each argument) it enters to an infinite loop. I understand that it has to do with the freeing of memory because it's getting stuck in that loop. And if a remove the last line (free(nodeLabHead);) it only gets stuck if I enter the file that has more nodes and then the one that has less, otherwise it works.

I also paid attention that, in the beginning, when allocating the memory to the first node, nodeLabHead->next==NULL returns 1 as expected, but then at the second time it gets there (with the second file, nodeLabHead->next==NULL returns 0, which means that it wasn't really freed, and I don't understand why.


Solution

  • You don't show your code, but it sounds like what is happening is that you never initialize nodeLabHead->next to be NULL, so it contains whatever happens to be in the memory that was allocated. It just so happens that the first time you call your function, it is 0, but later times it is not.

    The memory that you get back from malloc is uninitialized -- so might contain anything. You need to explicitly set every field of the struct if you want it it have a predictable value.

    Try changing the malloc call to calloc, or add a memset(nodeLabHead, 0, sizeof(labVar)) just after the malloc call.