VA_START called inside a function with fixed number of arguments

M

mahesha

What should be the behaviour of following program?. Is this behaviour
is undefined or compiler should report error for this case?. Can
anybody point me to pages in the standard where it is explained.

#include <stdarg.h>

void goo(int a) {
va_list args;
va_start(args, a);
}
int main() {
goo(2);
}
 
V

Victor Bazarov

mahesha said:
What should be the behaviour of following program?. Is this behaviour
is undefined or compiler should report error for this case?. Can
anybody point me to pages in the standard where it is explained.

#include <stdarg.h>

void goo(int a) {
va_list args;
va_start(args, a);
}
int main() {
goo(2);
}

In the C Standard (and also reiterated in the C++ Standard), it says
that *unless* the second argument to 'va_start' is the last named
argument before the "..." (", ..." in C), the behaviour is undefined.
Essentially we can conclude that the use of that macro in a function
that does not contain the ellipsis results in undefined behaviour,
since (like the 'a' in your example above) the argument would not be
"the last *before*" the ellipsis. That's the safest assumption I can
figure out.

V
 
L

Larry Smith

mahesha said:
What should be the behaviour of following program?. Is this behaviour
is undefined or compiler should report error for this case?. Can
anybody point me to pages in the standard where it is explained.

#include <stdarg.h>

void goo(int a) {
va_list args;
va_start(args, a);
}
int main() {
goo(2);
}

/* Note the '...' as the 2nd input parm to goo() */
void goo(int a, ...) {
va_list args;
va_start(args, a);
/* do stuff with args */
va_end(args);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top