Need help in <varargs.h>

Will Crowder willcr at bud.sos.ivy.isc.com
Fri Jun 14 09:04:52 AEST 1991


In article <1991Jun12.174326.5390 at Veritas.COM>, tsai at Veritas.COM (Cary Tsai)
writes:

|> Could you C guru tell me why the third ooprint() stmt causes coredump.
|> I must be missing something in ooprint().
|> Compile and execute 'a.out'. You'll get 'flag is on', 'flag is off' and
|> ...(coredump) or any bus error message.
|> 
|> [most of the source deleted]
|> 
|> 	va_start(ap);
|> 	foo = va_arg(ap, char *);
|> 	if ((func = va_arg(ap, PFV)) != (PFV) 0) {
|> 		if ((client = va_arg(ap, char *)) != (char *) 0)

It looks like you're expecting va_arg to return NULL if there is no argument.
This isn't how va_arg() works.  REMEMBER: In C, the called function has
no idea how many arguments you passed!  It generally has no way of
knowing, but that's not important.  The point is, as far as the language
definition is concerned, IT DOESN'T KNOW.

You must use either a sentinel value, an explicit argument count, or
some other method (such as counting the number of '%' in a printf() format
string) to determine how many times you can safely call va_arg() or use
the result.

I suspect what is happening is you are getting non-NULL garbage data from
your 

   ...func = va_arg(ap, PFV)...

and then garbage non-NULL data from

   ...client = va_arg(ap, char *)...

and then executing the garbage.

If you were to call it as

    ooprint("ooprint", (PFV) 0);

the third time it should work.

Hope this helps,

Will

P.S. (See the FAQ!)

--------------------------------------------------------------------------------
Will Crowder, MTS            | "That was setting #1.  Anyone want to see
(willcr at ivy.isc.com)         |  setting #2?"
INTERACTIVE Systems Corp.    |		-- Guinan



More information about the Comp.lang.c mailing list