HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :) 
Author Message
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)

Hi C gurus!!
   I need help on how to free memory which i allocate in a recursive function. The
code below shows that I m allocating memory to 'f' a struct node pointer. I need
to know how to free that memory at the end of all recursion.
  I understand if i free it at the end of each recursion., than I lose my data,
held in that pointer which i am storing in a linked list ! So sticking in a
free(f) in the function wont do it i suppose..So how do i free the enormous memory
which I allocate.. Please help.
Thanks in advance!!
Shriram

---------------------------------------------------------------------------------------
#include <stdio.h>

struct node {
  float point[3];
  struct node *next;

Quote:
};

main()
{
  /* assume all declareations of the program have been made correctly */
  /* find all the intersections */

  /* calling inters here */
  inters = recbox(box, q, p);

Quote:
}

struct node *recbox(float box[][3], float q[][3], float p[][3])

{

  /* variables */
  int i, flag;
  struct node *f[8], *f1;
  float b[8][8][3];
  float boxlength;
  float epsilon = 1.0;

  int twocurves(float box[][3], float q[][3], float p[][3]);
  void dividebox(float box[][3], float b[][8][3]);

  boxlength = 4 * (fabs(box[1][0] - box[0][0]) + fabs(box[3][1] - box[0][1]) +
                   fabs(box[4][2] - box[0][2]));

  printf("current boxlength is: %f\n",boxlength);

  /* determine if no intersection */
  if (!twocurves(box, q, p))
  {
    printf(" !2curves\n");
    return (NULL);
  }

  /* conclude an intersection */
  else if (boxlength < epsilon)
  {
    f[0] = (struct node *)malloc(sizeof(struct node)); /<---- ALLOCATING HERE!
    f[0]->point[0] = (box[1][0] + box[0][0])/2;
    f[0]->point[1] = (box[0][1] + box[3][1])/2;
    f[0]->point[2] = (box[0][2] + box[4][2])/2;
    f[0]->next = NULL;
    printf("\n we have an INTERSECTION!!!!!!!!!!!!!");
    printf("at points: %f     %f     %f\n",f[0]->point[0], f[0]->point[1],
            f[0]->point[2]);
    return (f[0]);
  }

  /* recursion to smaller and smaller boxes */

  else
  {
    dividebox(box, b);

    for (i=0;i<8;i++)
      f[i] = recbox(b[i], q, p); <------ RECURSIVE call

    append(f);   /* appends all the f[i]s to f[0] in single recursion */

    return(f[0]);
  }

Quote:
}

--
Shriram Venkatraman   -  I dream of the day when conservatives learn         -

   cs.montana.edu)    -  learn the difference between virtue and requirement -
------------------------------------------------------------------------------


Mon, 19 May 1997 14:25:41 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)
|> Hi C gurus!!
|>    I need help on how to free memory which i allocate in a recursive function. The
|> code below shows that I m allocating memory to 'f' a struct node pointer. I need
|> to know how to free that memory at the end of all recursion.
<snip>
|> struct node *recbox(float box[][3], float q[][3], float p[][3])
|>                    
|> {
<snip>
|>   struct node *f[8], *f1;
<snip>
|>   if (!twocurves(box, q, p))
|>   {
|>     printf(" !2curves\n");
|>     return (NULL);
|>   }
<snip>
|>   else if (boxlength < epsilon)
|>   {
|>     f[0] = (struct node *)malloc(sizeof(struct node)); /<---- ALLOCATING HERE!
<snip>
|>     return (f[0]);
|>   }
<snip>
|>   else
|>   {
<snip>
|>     for (i=0;i<8;i++)
|>       f[i] = recbox(b[i], q, p); <------ RECURSIVE call
|>
|>     append(f);   /* appends all the f[i]s to f[0] in single recursion */

Okay, here you have to do something smart.

1) make append reallocate f[0], and free f[1] thru f[7]. (free last returned
   value from recbox in the main program as well).
2) Malloc a global array of pointers (reallocate this if your recursion becomes
   too big) where you store f[1] thru f[7]. When you exit from the entire
   recursion, and after using contents of *f[0] etc.,
   free f[0] and all pointers stored in this global array, and then the global
   array itself.

|>
|>     return(f[0]);
|>   }
|> }
|>

Cheers
Tanmoy

--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Mon, 19 May 1997 23:40:40 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)

Quote:
>   I understand if i free it at the end of each recursion., than I lose my data,
> held in that pointer which i am storing in a linked list ! So sticking in a
> free(f) in the function wont do it i suppose..So how do i free the enormous memory

    You could try a code fragment like...

        {
        struct node *p,*next;
            for (p = inters; p; p = next) {
                next = p->next;
                free(p);
            }
        }

Quote:
> Shriram

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Tue, 20 May 1997 09:26:51 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)

Quote:

>    You could try a code fragment like...

>        {
>        struct node *p,*next;
>            for (p = inters; p; p = next) {
>                next = p->next;
>                free(p);
>            }
>        }

Is it true that some implementations of free()  went
to quite a bit of trouble to allow code like:

        for(p=list;p;p=p->next)
                free(p);

If so, are those implemenatations still common?

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Tue, 20 May 1997 18:03:14 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)


Quote:
>#include <stdio.h>

>struct node {
>  float point[3];
>  struct node *next;
>};

>main()
>{
>  /* assume all declareations of the program have been made correctly */
>  /* find all the intersections */

>  /* calling inters here */
>  inters = recbox(box, q, p);

[..snip..]

at this stage, after you have finished working with the 'inters' linked list,
you would I believe need to move through it, discarding each node. A brief
look at your program revealed very little - it wasn't ultimately readable,
especially as I don't understand how the algorithm works (!) but I came to
the conclusion that all the memory it allocates ends up in this
linked list that is returned. In this case, you would simply need to:

  struct node *int2,...;
  ....

  inters = recbox(box,q,p);

  int2 = inters;
  while(inters != NULL)
  {
    int2 = inters->next;
    free(inters);
    inters = int2;
  }

to move along the list, discarding each node.

I hope this helps.

--

   Flames should be redirected to /dev/null - I don't know what
   I'm saying myself so don't expect it to make sense all the time!         */



Wed, 21 May 1997 00:04:51 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)
<snip>
|> Is it true that some implementations of free()  went
|> to quite a bit of trouble to allow code like:
|>
|>   for(p=list;p;p=p->next)
|>           free(p);
|>
|> If so, are those implemenatations still common?

Most people define quality of implementation as the amount of broken code they
support :-)

In reality, such implementations exist, and are common on the machines I use
(SunOS etc.) I do not know about the rest.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Wed, 21 May 1997 00:17:25 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)

Quote:
> Is it true that some implementations of free()  went
> to quite a bit of trouble to allow code like:

>    for(p=list;p;p=p->next)
>            free(p);

    I don't know if anyone actually went out of their way to allow it.
    Maybe some lost souls on experimental machines like the Japanese
    Prolog one? I'm sure people in that unfortunate position would
    have much worse problems than this to contend with :), because
    unless the system architecture is bizarre it's easier to allow
    this code than it is to prevent it.

    It seems to me that any implementation meeting the following
    conditions will allow this code...

    1.  free() doesn't destroy the contents of the freed block, or at
        least doesn't destroy the part of the block containing "next".
        The model described in K&R meets this condition.

    2.  free() doesn't return the block to the OS, so no subsequent
        range checking can be performed by the OS. The model in K&R
        meets this condition also.

    3.  Pointers are implemented as simple memory address values with
        no reference validation performed by the compiled code. I
        would wildly surmise that all commercial implementations on
        common CPUs like the 680x0 and 80x86 series meet this
        condition.

Quote:
> If so, are those implemenatations still common?

    Well, it seems likely that most implementations are exactly like
    this. Debugging versions of the memory management functions should
    catch it, though, as might smart compilers and lints.

Quote:
> Miguel Carrasquer         ____________________  ~~~

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Wed, 21 May 1997 16:11:17 GMT  
 HELP!!! on Freeing memory in RECURSION!!! PLEASSSE urgent!! :)

Quote:


>> Is it true that some implementations of free() went
>> to quite a bit of trouble to allow code like:

>>        for(p=list;p;p=p->next)
>>                free(p);

>    It seems to me that any implementation meeting the following
>    conditions will allow this code...  [Conditions omitted.]
>    [I]t seems likely that most implementations are exactly like
>    this.

For a striking counter-example to this last statement, see the memory
allocator used in the Andrew project at Carnegie Mellon.  The source
code is available on-line; the code is based on algorithms 6.6(b) and 6.7
in
        E.M. Reingold, W.J. Hansen
        Data Structures in Pascal
        Little, Brown
        1986

This is a very, very good memory allocator, with several notable
characteristics:
        allocation is fast
        free() is O(1)  (i.e., it takes constant time, regardless of
                how many blocks have been allocated or freed)
        the overhead in allocated blocks in sizeof(void *), typically
                four bytes

Pretty impressive technology!  Importantly, this algorithm coalesces
adjacent free blocks at free() time -- and in doing so, it USES some of
the free memory, thus trashing anything the user might hope to be still
able to use after the free() call returns.

Since memory allocators are notorious for consuming large amounts of
CPU time, we should expect to see algorithms like the one above
implemented more and more often; so depending on the (broken) code above
is probably not a good idea.




Tue, 03 Jun 1997 02:21:13 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. HELP ME on Freeing Memory during RECURSION!!!!!

2. URGENT HELP WITH TREES and recursion

3. Help with memory alocation--URGENT!!!

4. Help needed for fixing Memory Leak in program (URGENT)

5. can I know the amount of memory freed by call to free

6. Help with memory alocation-Urgent!!!

7. how free realy frees memory ?

8. Want to write into memory adress under Visual C++ - Need help very urgent

9. Problems freeing memory using "free"

10. free() is not releasing memory, help

11. MEMORY ERROR: Need help to free Extended RAM

12. Please, help with checking memory allocation free soft

 

 
Powered by phpBB® Forum Software