On Nov 11, 9:16 pm, saurabh singh <saurab...@gmail.com> wrote: > well that would be tough for the compiler to predict things that will > happen during run time.Its the job of garbage collector to do that. well compiler cant predict what will happen at runtime. > > > > > > > > > > On Fri, Nov 11, 2011 at 8:36 PM, shady <sinv...@gmail.com> wrote: > > ok, thanks. > > > why do we need to free the memory ? if you do not free the memory, you will run out of the memory.
> > Suppose i have a linked list of 1000 nodes and i make the head of it = > > NULL, thus losing the whole list. Then compiler can look at other variables > > and if this list has not been referenced anywhere else then it is useless, > > thus will free the memory. > > Is the argument wrong ? > > > On Fri, Nov 11, 2011 at 8:20 PM, vikas <vikas.rastogi2...@gmail.com>wrote: > > >> nopes , they are not connected, it is just a chance you are getting > >> the same values and nothing is overwritten there: basically these are > >> DANGLING POINTERS . Now you should keep practising something like this > > >> #define FREE(N) { free(N); N=NULL;} > > >> to avoid such mistakes > > >> On Nov 11, 3:41 pm, shady <sinv...@gmail.com> wrote: > >> > typedef struct n{ > >> > int num; > >> > struct n *next; > > >> > }node; > > >> > node is the structure to create the linked list. > > >> > node *list1; > > >> > I have created a linked list ( list1 )like this 1 -> 2 -> 3 -> 4 > > >> > so i free it like this ---- > > >> > free(list1 -> next -> next ->next); > >> > free(list1 -> next -> next); > >> > free(list1 -> next); > >> > free(list1); > > >> > when i am printing the list after each free, it is always printing a > >> > list of length 4, isn't the values free'd when we do free() ? > > >> > actual printing gives > >> > 1 2 3 0 > >> > 1 2 garbage 0 > >> > 1 garbage garbage 0 > >> > garbage garbage garbage 0 > > >> > why is the linked list still connected ? > > >> > actual print function ----- > >> > void print(node *l) > >> > { > >> > while(l != NULL) > >> > { > >> > printf("%d\t",l->num); > >> > l = l->next; > >> > } > >> > printf("\n"); > > >> > } > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algogeeks@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com. > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.