🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2152

Re: Variable param for non-vararg subr. calls

#2152
From: Andrew Smith
Date: Sat, 02 Mar 1996 14:39
61 lines
2667 bytes
John Shalf wrote:
> Varargs allows you to unpack a variable number of arguments from a
> subroutine call at runtime.  You would think there would be a way to
> *pack* a variable number of arguments for a subroutine call at runtime.
> However the C faq says;
>[snip]
> no! :)
>[/snip]
> 
> Has anyone come up with a saner, more portable method for generating
> subroutines calls with a variable number of arguments at runtime?
> 

Im curious, is there no way for you to design your application so that
you could load your parameters into a structure or array?  As a possible
scenario howabout something like this (on the fly) example:

struct arg_list
{
   int ptype;
   void *pval;
};

struct arg_list **plist;

   /*---------------------------------------------------------------*/
   /* Now for PNUM args we need space to point to PNUM of the above */
   /*---------------------------------------------------------------*/
   plist = (arg_list **)malloc(sizeof(*plist)*PNUM); /* List */

   /*----------------------------------------------------------------*/
   /* And now malloc the structure itself for each param.  Excuse me */
   /* for dumb mistakes here... alright?                             */
   /*----------------------------------------------------------------*/
   for (i=0;i<PNUM;i++)
   {
      plist[i]=(arg_list *)malloc(sizeof(**plist)); /* Structure */

      /*------------------------------------------*/
      /* And now, the space for each param itself */
      /*------------------------------------------*/
      plist[i]->pval=(void *)malloc(PARAM_SIZE);

      /*----------------------------*/
      /* Put value in the structure */
      /*----------------------------*/
      plist[i]->ptype = ENUMERATED_TYPE;
      memcpy(plist[i]->pval,PARAM_ADDRESS);
   }

   /*-----------------------------*/
   /* Finally, call some function */
   /*-----------------------------*/
   varparmfunc(PNUM,plist);

The concept should work, if not this exact code... and if you do implement
something like this make sure you check that your malloc works!

Andrew Smith
Custom Innovative Solutions
http://www.cisc.com/
Technical Details
Message-ID: <4h9mju$gsa@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4gsaai$41m@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Andrew Smith <asmith@cisc.com>
Newsgroups: comp.lang.c.moderated
Subject: Re: Variable param for non-vararg subr. calls
Date: 2 Mar 1996 08:39:58 -0600
Organization: Custom Innovative Solutions
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4h9mju$gsa@solutions.solon.com>
References: <4gsaai$41m@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Mailer: Mozilla 2.0 (Win95; I)