variable number of strings passed t

Gordon Cross crossgl at ingr.UUCP
Tue Nov 1 03:08:30 AEST 1988


In article <225800083 at uxe.cso.uiuc.edu>, mcdonald at uxe.cso.uiuc.edu writes:
> Oh come on now. How about mallocing up enough space to put an array
> of pointers to the strings in memory and passing a pointer to that array?
> That is portable and avoids varargs or stdarg.

Yes this is true:  portability is maintained and you avoid the (messy??)
varargs convention.  However, I would NOT recommend using malloc in this
fashion for two reasons:

1) First, mallocing memory is usually a costly (timewise) undertaking when
   compared to the time it takes to pass arguments.  Most decent compilers
   should pass arguments using the most efficient method possible.  If you
   are calling such a function many times, you may find yourself spending
   all of your time mallocing and freeing!

2) Your code would be "messy" (descriptive term) in the areas where you call
   this function because you would have to do this:

        buffer = (char **)malloc (size);
        buffer[0] = "arg1";
        buffer[1] = "arg2";
           .
           .
           .
        buffer[n] = 0;
        yourfunction (buffer);
        free (buffer);

   If you have to do this in very many different places, your code size could
   adversely affected.  Wouldn't it be much nicer to be able to call the 
   function with the single line

        yourfunction ("arg1", "arg2", ... , 0);


Gordon Cross
Intergraph Corp.  Huntsville, AL



More information about the Comp.lang.c mailing list