VA_START called inside a function with fixed number of arguments

Discussion in 'C++' started by mahesha, Oct 5, 2006.

  1. mahesha

    mahesha Guest

    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);
    }
     
    mahesha, Oct 5, 2006
    #1
    1. Advertising

  2. mahesha wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 5, 2006
    #2
    1. Advertising

  3. mahesha

    Larry Smith Guest

    mahesha wrote:
    > 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);
    }
     
    Larry Smith, Oct 5, 2006
    #3
  4. mahesha

    Larry Smith Guest

    mahesha wrote:
    > 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);
    > }
    >



    http://www.penguin-soft.com/penguin/man/3/stdarg.html
     
    Larry Smith, Oct 5, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frankg
    Replies:
    4
    Views:
    417
    Joe Durusau
    Jul 28, 2003
  2. Minti

    Why va_start takes 2 arguments

    Minti, Sep 3, 2004, in forum: C Programming
    Replies:
    1
    Views:
    382
    Ben Pfaff
    Sep 3, 2004
  3. jmborr
    Replies:
    1
    Views:
    459
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    866
  5. tukki
    Replies:
    0
    Views:
    366
    tukki
    Jul 30, 2010
Loading...

Share This Page