LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   free()'n args (https://www.linuxquestions.org/questions/programming-9/free-n-args-109229/)

zer0python 10-27-2003 04:53 PM

free()'n args
 
I'm not sure if it is possible, but I want to make a function called freeAll (hopefully that is self explanitory) using a va_list, I'm not sure if this is possible, could someone help me out on this, anyway.. my reasoning is I have about 7, or 8.. variables malloc'd, and when an error occurs I have to free them all, but I think it would be easier if I could just do something like ->

freeAll(malloc_one, malloc_two, malloc_three, ...) then having to do ->

free(malloc_one);
free(malloc_two);
free(malloc_three);

I know I should've searched a little longer, and probably hunted the forums, but it's hard to phrase this in a search engine..

jim mcnamara 10-27-2003 05:02 PM

Code:

void freeAll(void **ptr, int count){
    int i=0;
    for(i=0;i<count;i++)free(ptr[i]);
}


zer0python 10-27-2003 05:07 PM

multi-demensional array? hmm, that could work, but I meant passing each variable.. something like:

Code:


void freeAll(int c, void *q, ...)
{
    va_args args;
    int j;

    va_start(args, q);
    for(j = 0; j < c; ++j)
        free(va_arg(args, char*));
}



All times are GMT -5. The time now is 10:55 AM.