Thread: Free the pointer

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Free the pointer

    Hello,

    I need to write a function such that it frees all the pointers that I allocate.My code looks like this:

    Code:
    int someFunction()
    {
    //Allocating pointers.
    szTemp1 = calloc(10, sizeof(char));
    stTemp2 = calloc(10 , sizeof(char));
    stTemp3 = calloc (10 , sizeof(char));
    
    //Logic goes here
    if ( something bad happened)
    {
    //free all pointers
    free(szTemp1 );
    free(szTemp2 );
    free(szTemp3 );
    return FAIL;
    }
    
    if( something else goes wrong)
    {
    //free all pointers
    free(szTemp1 );
    free(szTemp2 );
    free(szTemp3 );
    return FAIL;
    }
    
    //Finally in the end:
    
    //free all pointers
    free(szTemp1 );
    free(szTemp2 );
    free(szTemp3 );
    return SUCCESS;
    }

    Here , instead of repeating the same lines to free the pointers everytime in the code, I would like to call a function which releases all pointers. How would i do it? I would not want to use the goto statement to achieve the same.

    Please help.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I think it should be possible to write a function that accepts pointers as arguments and frees them. Something like:

    Code:
    void freeMultiplePointers(char *p1, char *p2, char *p3)
    {
        free(p1);
        free(p2);
        free(p3);
    }
    To make this function compatible with multiple types you can use void *p1, etc as arguments. I am not totally sure about this solution though.
    Last edited by Ideswa; 03-20-2009 at 03:43 AM.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So long as all the arguments are allocated by malloc/calloc, then it doesn't matter what type you pass to the function because free accepts void*, so yes, it's possible to use void* in the parameter list.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM