user220201
user220201

Reputation: 4534

variable arg functions and errors

I have a variable arg function with the following signature --

int G( int a, char *b, int c, ... );

The caller can pass in an option int d if they so choose. If not we use the default for d.

There is no relationship between (a,b,c) and d. What I mean by that is neither of the parameters a, b or c tell me if d is going to be passed in or not.

In my code I am doing this --

va_list valist;    
int input_d = 0;
va_start( valist, c);
input_d = va_arg(va_list, int);

Is it possible for va_arg() function to return a garbage value if the caller only passed in a, b and c (only three parameters)?

Upvotes: 0

Views: 84

Answers (1)

aschepler
aschepler

Reputation: 72336

Yes, calling va_arg for a non-existent argument is Undefined Behavior, so you could get a garbage value, a crash, or anything at all. A varargs function must know the number and types of its variadic arguments.

Upvotes: 1

Related Questions