Saturday 15 March 2014

null - Linked List in C Searching/Inserting -



null - Linked List in C Searching/Inserting -

i'm trying write code emulating mmu in computer (kind of irrelevant), , after going through breakpoints in debugger i've found i'm having troubles linked list searching , inserting.

if don't include line "table->first = null;" access violation error because skips function within "if(table->first == null){" in insert function.

also, when seek utilize search function (after inserting 1 node list), while loop goes through sec iteration (even though there shouldn't table->current points to) , causes access violation on line "sum = table->current->end - table->current->start;"

is there linked lists , null value i'm missing? i've tried alter while loop status (in search function) "while(table->current)" , "while(table->current != 0)" , neither alter made difference. can ignore of pthread parts, have them commented out because debugging on windows machine/haven't got part yet.

thank advice can give!

#include <stdio.h> #include <stdlib.h> #include <string.h> //#include <pthread.h> #define max_size 20 struct node //nodes double linked list { int start; int end; int free; //tags removal struct node *prev; struct node *next; }; typedef struct node node; typedef struct //double linked list struct page table { node *first; node *last; node *current; int is_available; //determines whether or not plenty space available } page_table; //pthread_mutex_t mux; page_table* table; void insert(node *old, node *newnode, int size); void remove(node *node); node* search(int size); void tagremove(int size); int main() { table = (page_table*) malloc(sizeof(page_table)); table->first = null; table->last = null; node* node1 = (node*) malloc(sizeof(node)); node* node2 = (node*) malloc(sizeof(node)); node* node3 = (node*) malloc(sizeof(node)); node* node4 = (node*) malloc(sizeof(node)); insert(null, node1, 2); node* temp = search(3); //insert(temp, node2, 3); //temp = search(4); //insert(temp, node3, 4); } void insert(node *old, node *newnode, int size) { if(table->first == null){ table->first = newnode; table->last = newnode; newnode->start = 0; } else if(old->next == null){ old->next = newnode; newnode->prev = old; table->last = newnode; newnode->start = old->end; } else if(old == table->first){ newnode->next = table->first; table->first->prev = newnode; table->first = newnode; newnode->start = 0; } else{ newnode->next = old->next; old->next = newnode; newnode->prev = old; newnode->start = old->end; } newnode->end = newnode->start + size; newnode->free = 0; table->current = newnode; } void remove(node *node) { if(node == table->first && node == table->last) { table->first = null; table->last = null; } else if(node == table->first) { table->first = node->next; table->first->prev = null; } else if (node == table->last) { table->last = node->prev; table->last->next = null; } else { node->next->prev = node->prev; node->prev->next = node->next; } } node* search(int size){ table->current = table->first; int temp_size; int sum = 0; while(table->current != null){ temp_size = 0; sum = table->current->end - table->current->start; if(sum>=max_size) homecoming null; temp_size = table->current->end - table->current->start; table->current = table->current->next; if(size<=temp_size) homecoming table->current->prev; } homecoming 0; } void tagremove(int size) { int sum; node* temp = table->first; sum = temp->end - temp->start; temp->free = 1; while(temp != null){ temp = temp->next; sum += temp->end - temp->start; temp->free = 1; if(sum < size) return; } }

you'll save lot of problem if utilize calloc instead of malloc. calloc clears memory allocates zeroes, , malloc not. quick glance @ code, looks expected allocated memory zeroed.

case in point, node1->next , node1->prev uninitialized after insert node1 table utilize them during search.

c null doubly-linked-list

No comments:

Post a Comment