[algogeeks] Re: free() function

2011-11-13 Thread Gene
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  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  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  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.



Re: [algogeeks] Re: free() function

2011-11-13 Thread Debabrata Das
process heap is organized as chunk of free memory need not be
contiguous and during  freeing what free() does is update it's book
keeping records and it's bins array so that next time if asked for new
space may return the same space however it doesn't wipe the freed data
but few run time directive are present in few rtdl libary to overwrite
the freed space with some poison value ( configurable )

On Sun, Nov 13, 2011 at 11:52 PM, sumit mahamuni
 wrote:
>
>
> On Nov 11, 9:16 pm, saurabh singh  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  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 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  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.
>
>

-- 
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.



[algogeeks] Re: free() function

2011-11-13 Thread sumit mahamuni


On Nov 11, 9:16 pm, saurabh singh  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  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 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  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.



Re: [algogeeks] Re: free() function

2011-11-11 Thread saurabh singh
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.

On Fri, Nov 11, 2011 at 8:36 PM, shady  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 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  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.



Re: [algogeeks] Re: free() function

2011-11-11 Thread shady
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  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  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.



[algogeeks] Re: free() function

2011-11-11 Thread vikas
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  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.