Not meaning any disrespect, this argument is about as wrong as it gets in computer science.
Predicting when memory will be free in a running program at compile time is called an _undecidable problem_. This means it's totally impossible to write a C / C++ compiler that will produce an executable that always calls free() at the right place. I'm not saying it's just hard. It's mathematically impossible. You _can_ write compilers that get the places some of the time. Usually such algorithms are "conservative." In this case, conservative means that the compiler could generate free() whenever it can prove absolutely that memory is no longer in use. In the two others other possible cases 1) memory is really still in use 2) memory is not in use, but the compiler's algorithm can't prove it, memory is not freed. Case 2) will of course cause a memory leak. Some language systems do free memory automatically, but this is done at run time by a special algorithm that runs occassionally or concurrently with the program itself. The algorithm finds and analyzes all accessible pointers then frees allocated memory that's no longer referenced. This is called _garbage collection_. Java, C#, lisp, perl, python, OCAML are all garbage-collected languages. Your basic thought is good in this sense. Garbage collection is relatively expensive, so some compilers do an optimization called "pointer escape" that allows them to allocate objects on the stack rather than the heap. Getting this right 100% of the time is undecidable, so ... you guessed it ... the optimization is implemented conservatively. On Nov 11, 10:06 am, shady <sinv...@gmail.com> wrote: > ok, thanks. > > why do we need to free 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.