hi guys please help about Linked List,
I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is not working??? You can see the difference if you monitor the Memory and Swap in your OS System Monitor.
Thanks in advance!
Here is my code
I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is not working??? You can see the difference if you monitor the Memory and Swap in your OS System Monitor.
Thanks in advance!
Here is my code
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> /* Base Platform OS: Ubuntu 7.04 C Compiler : gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4) How I compile : terminal$ gcc -Wall main.c -o main Problem : Freeing the allocated memory of Dynamic Char* in a Single Linked List is not working I don't know why.. But if I use the Static Char Array and free the Single Linked List Nodes, the memory and swap allocated are freed up successfuly. Note : To monitor the behaviour of the Memory and the Swap memory of the OS use the System Monitor */ typedef struct ptable { char *data1;//[50]; char *data2;//[50]; char *data3;//[50]; char *data4;//[50]; int len; struct ptable *next; }pTable; /* function prototypes */ void freemem(pTable **pointer); void drop_rec(char * rec); int display(pTable *pointer); /* Free the allocated memory of Dynamic Char* */ void drop_rec(char * rec) { free(rec); rec = NULL; return; } int main() { long i = 0; char tdata[50] = ""; pTable * ptable; pTable * header; pTable * pointer; memset(tdata,0,50); /* create a list of data */ for(i=0;i<=1000000;i++) { /* clean tdata first */ memset(tdata,0,50); /* create data to be inserted */ sprintf(tdata,"0000000%ld",i); /* allocate memory */ ptable = (pTable *) malloc(sizeof(pTable)); if(ptable == NULL){ printf("Unable to allocate memory.\n"); break; return -1; } /* I should also check these,if the memory has been allocated successfuly */ /* but I want to make this code short just to understand quickly*/ ptable->data1 = malloc(strlen((char *) tdata)+1); /* set the size to be allocated */ ptable->data2 = malloc(strlen((char *) tdata)+1); /* set the size to be allocated */ ptable->data3 = malloc(strlen((char *) tdata)+1); /* set the size to be allocated */ ptable->data4 = malloc(strlen((char *) tdata)+1); /* set the size to be allocated */ ptable->next = NULL; if(header == NULL) header = ptable; else pointer->next = (pTable *) ptable; /* Assign some values */ sprintf(ptable->data1,"%s",(char *) tdata); sprintf(ptable->data2,"%s",(char *) tdata); sprintf(ptable->data3,"%s",(char *) tdata); sprintf(ptable->data4,"%s",(char *) tdata); pointer = ptable; } pointer = header; /* displaying the records will take some time */ /* display(pointer); */ printf("Press Any Key to release allocated memory . . .\n"); getchar(); printf("freeing allocated memory . . .\n"); printf("Look at the System Monitor if the Memory and Swaps are being freed.\n"); freemem(&pointer); /* free memory (this one is not working I don't know) */ freemem(&header); /* free memory (this one is not working I don't know) */ printf("Memory freed up! Done.\n"); printf("Press Any Key to close.\n"); getchar(); /* then it only freed up the allocated memory if the program is terminated */ return 0; } /* free up memory doesn't work */ void freemem(pTable **pointer) { pTable *ptr,*next; ptr = *pointer; while(ptr != NULL) { /* save the next node */ next =(pTable*) ptr->next; /* defective part of my freemem (NOT Working)*/ /* I used the FREE() function, same result doesn't work*/ drop_rec(ptr->data1); /* free the char* field */ drop_rec(ptr->data2); /* free the char* field */ drop_rec(ptr->data3); /* free the char* field */ drop_rec(ptr->data4); /* free the char* field */ free(ptr); /* free node */ ptr = next; /* get the next node to be freed up*/ } /* remove the handle*/ ptr = NULL; return; } int display(pTable *pointer) { pTable *ptr; ptr = pointer; printf("\n\n"); /* loop and display records */ while(ptr != NULL) { printf("************\n"); printf("Data1 [%s]\n",ptr->data1); printf("Data2 [%s]\n",ptr->data2); printf("Data3 [%s]\n",ptr->data3); printf("Data4 [%s]\n",ptr->data4); ptr = (pTable*) ptr->next; printf("************\n"); } printf("\n\n"); return 0; }
Comment