Weird values in free() function C

Viewed 63

I want to delete the first node of a linked list (You can see below the image) enter image description here

Τhe structures are as follows:

typedef struct PageEntry {  //Node
    unsigned int page_number;
    char mode;
    int count, R;
    struct PageEntry *next;
}PE;


typedef struct Element {
    int val;
    PE* pe;
}Element;

typedef struct PageTable {
    int p_faults, reads, writes, disk_writes, maxFrames, usedFrames;
    char* algorithm;
    Element* el;
}PT;

My code for trying delete the first node is here.

PE *cur = pt->el[pos].pe;
PE *prev =NULL, *temp = cur;
if(cur->count == min){  //head node
    if(cur->mode == 'W'){
        pt->disk_writes++;
            
    }
    if (cur->next == NULL) {
        memset(cur, 0, sizeof(PE));
        free(pt->el[pos].pe);
        cur = NULL;
    }
    else {
        cur = temp->next;
        free(temp);
    }

I am working with Visual Studio and when i do free i get back some weird values as you can see at the sceenshot. I cant unsterstand what's happening enter image description here

1 Answers

Your diagram does not describe a list, but an array of lists. And that's fine in itself. But - why are you mixing up the code for an actual list with the code of working with the page table as a whole? It's not clear where exactly you decide you need to delete a list node (i.e. a page entry). You're also implicitly assuming the list is not empty. You're memsetting needlessly. Finally, and most importantly - when you free(temp) - you don't set pt->el[pos].pe to point to cur->next. So, it continues to point to the element you've just freed.

Related