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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top