Free memory or garbage in recursion program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Free memory or garbage in recursion program

Hybrid View

  1. #1
    Join Date
    Jul 2005
    Posts
    9

    Free memory or garbage in recursion program

    /*
    ** Free memory or garbage in recursion program
    ** Here, i have maked a simple recursion program. It's parametter is
    ** an integer called i.
    ** If i = 5 then return 1, else return i * Recursion(i+1). It is
    ** very simple, we will not say about the mean of this program.
    ** The more important, int Recursion function i created a j varible
    ** and when the program finished, i want to release memories pointed
    ** by pointer j. I don't know how. Can you help me.
    ** I want to know your idea and don't change my idea in this funny
    ** program.
    ** I think we always met this problem.
    ** Good luck to all of you !
    */
    #include <stdio.h>
    #include <malloc.h>

    int Recursion( int* i)
    {
    if ( *i == 5 )
    return 1;
    else
    {
    int *j = 0;
    j = (int*) malloc(sizeof(int));
    *j = *i+1;
    return (*i * Recursion( j ));
    }
    }
    void main()
    {

    int i = 1;
    int k = Recursion( &i );

    printf("%d",k); // return 24


    }

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    No can do. Or in English: it's impossible. You're using a different copy of j everytime you call Recursion and since the value (i.e., the address) assigned to it is different everytime you call malloc, there's no way to release five different pointers when you have only one pointer.
    To accomplish this task you need a data structure such as a linked list of vector (or even ab array) that stores on each call the newly allocated address.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    which can be done with a static variable in the recursive function (the list of pointers)

    or you can make the pointer static and allocate it once, and free it once. (you can even re-alloc it each time if you want, slow but recursion is slow anyway).
    Since Im not motivated to figure out what the code did, that might do for your app or not depending on if you *need* all the pointers (one per iteration) or not.
    Last edited by jonnin; 08-02-2005 at 07:12 AM.

  4. #4
    Join Date
    Aug 2005
    Location
    Jamaica, W.I.
    Posts
    4
    This is it, if that is what youre getting at.
    _________________________________________________________________

    /*
    ** Free memory or garbage in recursion program
    ** Here, i have maked a simple recursion program. It's parametter is
    ** an integer called i.
    ** If i = 5 then return 1, else return i * Recursion(i+1). It is
    ** very simple, we will not say about the mean of this program.
    ** The more important, int Recursion function i created a j varible
    ** and when the program finished, i want to release memories pointed
    ** by pointer j. I don't know how. Can you help me.
    ** I want to know your idea and don't change my idea in this funny
    ** program.
    ** I think we always met this problem.
    ** Good luck to all of you !
    */
    #include <stdio.h>
    #include <malloc.h>

    int Recursion( int* i)
    {
    if ( *i == 5 ) return 1;
    else
    {
    int *j = 0, retVal; // declare return value
    j = (int*) malloc( sizeof( int ) );
    *j = *i+1;
    retVal = *i * Recursion( j ); // find return value
    free( j ); // free up the precious resource
    return (retVal);
    }
    }

    void main()
    {

    int i = 1;
    int k = Recursion( &i );

    printf("%d",k); // return 24
    }

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    That last code listing is leak free. Nice!
    Last edited by Danny; 08-03-2005 at 10:40 PM.
    Danny Kalev

  6. #6
    Join Date
    Aug 2005
    Location
    Jamaica, W.I.
    Posts
    4
    are you talking about my solution?

  7. #7
    Join Date
    Jul 2005
    Posts
    9
    Thanks you. I am thinking about your solutions, after 3 days i will up my solution.

  8. #8
    Join Date
    Aug 2005
    Location
    Jamaica, W.I.
    Posts
    4
    There are, of course, less elegant and more obfuscated ways of doing it.

    UGLY METHOD 1
    _____________________________________________________________
    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    int Recursion( int* i)
    {
    if ( *i == 5 )
    return 1;
    else
    {
    int *j = 0;
    j = (int*) malloc(sizeof(int));
    *j = *i+1;
    return (*i * Recursion( j ));
    }
    }
    
    void Fixit( int* i)
    {
    if ( *i == 5 )
    return 1;
    else
    {
    int *j;
    free(j);
    Recursion( i );
    }
    }
    
    void main()
    {
    int i = 1;
    int k = Recursion( &i );
    Fixit(&i);
    
    printf("%d",k); // return 24
    }

    UGLY METHOD 2
    _______________________________________________________________
    Code:
    int Recursion( int* i)
    {
    if ( *i == 5 )
    return 1;
    else
    {
    int *j = 0;
    j = (int*) malloc(sizeof(int));
    *j = *i+1;
    return (*i * Recursion( j ) + ( free(j), 0 ) );
    }
    }
    
    
    void main()
    {
    int i = 1;
    int k = Recursion( &i );
    Fixit(&i);
    
    printf("%d",k); // return 24
    }

Similar Threads

  1. Replies: 0
    Last Post: 07-14-2003, 03:08 PM
  2. You'll Earn $10 Just For Signing Up...FREE!
    By smrutideep in forum Careers
    Replies: 2
    Last Post: 03-20-2003, 12:39 AM
  3. Replies: 0
    Last Post: 12-15-2000, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center