Thread: Where to put free in my program?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Where to put free in my program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include <crtdbg.h>  // do not remove
    
    #define FLUSH while (getchar() != '\n')
    #define MEM_ERROR printf("Not enough memory!\n")
    
    // Prototype Declarations
    double getNum(void);
    double getNumbers(double **pptr);
    double insertionSort(double **pptr, int counter);
    double printTable(double *ptr, double **pptr, int counter);
    
    int main (void)
    {
           //Local Definitions
           double *ptr;
           double **pptr;
           int counter = 0;
           int i;
    
           //  Statements
           printf("\t\t Homework 3 - Pointer Applications:\n"
                      "\t\t    Insertion Sort and Pointers\n\n" );
           if(!(ptr = malloc(1000 * sizeof (double)))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
           counter = getNumbers(&ptr);// if input is too big
    
           ptr = realloc(ptr, counter * sizeof(double));
           if( pptr == NULL )
              MEM_ERROR;
    
    
           if(!(pptr = (double**) malloc(counter * sizeof ( double* )))){
                   printf("Not enough memory available.\n"); // no memory available
                   exit(100);
           }
    
           for(i = 0; i < counter; i++)
                   pptr[i] = &(ptr[i]);
    
           insertionSort(pptr, counter);
           printTable(ptr, pptr, counter);
           //  printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");  // do not remove
           printf("\n\t\tEnd of Program\n"
                      "\n\t\tHave a great day!\n");
    
           return 0;
    
    }
    
    /************************************************
     Prompt the user to enter a valid integer number
     Pre: nothing
     Post: returns a valid integer
    */
    double getNum(void)
    {
           double num;
           while (scanf("%lg", &num) != 1)
           {
                         // scanf returns 1 if number read corrrectly
               FLUSH;    // to "consume" the invalid input
              printf("-*- Invalid number. Please re-enter: ");
           } // while
    
    
       return num;
    }
    
    
    double getNumbers(double **pptr)
    {
    int counter = 0;
    double number;
    
    
           printf("\nPlease enter numbers and enter -1 when you are done: ");
           number = getNum();
           while(number!= -1){
                   (*pptr)[counter] = number;
                   number = getNum();
                   counter++;
    
           }
    
           return counter;
    }
    //This function sorts the array of pointer into ascending order.
    double insertionSort(double **pptr, int counter)
    {
    int i, j;
    double *temp;
    
           for(i = 1; i < counter; i++)
           {
                   temp = pptr[i];
                   j = i - 1;
                           while(j >= 0 && *temp < *pptr[j])
                           {
                                   pptr[j + 1] = pptr[j];
                                   j = j - 1;
                           }
                   pptr[j + 1] = temp;
           }//insertion sort
    return;
    }
    /*This function prints the array in ascending and descending order
    as well as the original order that the user put in*/
    double printTable(double *ptr, double **pptr, int counter)
    {
    int i;
           printf("\nAscending\tOriginal\tDescending\n");
           for(i = 0; i < counter; i++)
           {
                   printf("%lg\t\t%lg\t\t%lg\n", *(pptr[i]), ptr[i], *(pptr[counter - 1 - i]));
           }
    
    }
    -Okay, so I know that I need to use free twice in main(for ptr and pptr). But I am not sure where to put it. Is there a specific place? At the very end of main, or after allocating the memory?
    Thank you for your time.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    When you no longer need to use the memory pointed to by those pointers you want to free. Simple as that.

    And please stop casting the return values of malloc. It's just not necessary.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by rags_to_riches View Post
    When you no longer need to use the memory pointed to by those pointers you want to free. Simple as that.
    But, I'm not sure when that is. I don't want to crash the program. Intuitively, I want to put it at the very bottom on my program, but I know that I need to put it in main.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Hmm I put both of the frees in main after my printTable function call. Is that reasonable? The program worked fine when I ran it.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That's fine. However I suggest you turn your warnings on or up if necessary and pay attention to them, particularly the one about using an uninitialised varaible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by iMalc View Post
    That's fine. However I suggest you turn your warnings on or up if necessary and pay attention to them, particularly the one about using an uninitialised varaible.
    I'm running it in Unix and its not giving me any warnings... and seems to work fine.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by hotshotennis View Post
    I'm running it in Unix and its not giving me any warnings... and seems to work fine.
    It has 3 warnings when I compile it.

    I suggest you fix the causes of the warnings.

    This is the option needed to turn on the normal warnings I compile with using GCC.

    Code:
     -Wall
    Tim S.
    Last edited by stahta01; 05-15-2012 at 01:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I just get a bunch of "previously declared" statements that make no sense.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by hotshotennis View Post
    I just get a bunch of "previously declared" statements that make no sense.
    You do realize that the warnings and errors you get do make sense!

    I suggest posting the command used to compiler/link the source code along with the errors/warnings.
    This along with your source code could result in somebody helping you.

    You stating that you do not understand the messages will NOT result in anyone helping you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It might appear to work... sometimes... but an uninitialised variable in your program means that it will sometimes print out "Not enough memory!" at random.
    If you allow the compiler to generate warnings, then it will show you how to fix that bug.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by hotshotennis View Post
    I just get a bunch of "previously declared" statements that make no sense.
    To you maybe, but when it comes to generating machine code from C, the compiler is way smarter than you.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free art program
    By joed in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-01-2006, 06:30 PM
  2. Best free c++ program?
    By Warlock in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2006, 08:46 AM
  3. How to free memory in this program
    By Coconut in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 10:57 PM
  4. Which is the free drawing program?
    By Tazar_Azar in forum Game Programming
    Replies: 5
    Last Post: 03-05-2002, 09:34 AM