Thread: Variadic function weirdness

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Variadic function weirdness

    I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?

    For example, the following code outputs
    Code:
    1 2
    2 1
    Code:
    #include <iostream>
    #include <stdarg.h>
    
    void foo_func(int v1, int v2)
    {
      std::cout << v1 << " " << v2 << std::endl;
    }
    
    void myfunc(int argc, ...)
    {
      va_list args;
      va_start(args, argc);
    
      int val1 = va_arg(args, int);
      int val2 = va_arg(args, int);
    
      foo_func(val1, val2);
    
      va_end(args);
    }
    
    void myfunc2(int argc, ...)
    {
      va_list args;
      va_start(args, argc);
    
      foo_func(va_arg(args, int), va_arg(args, int));
      va_end(args);
    }
    
    int main ()
    {
      myfunc(2, 1, 2);
      myfunc2(2, 1, 2);
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The order in which arguments are evaluated is implementation-defined (technically, by the standard, it's unspecified). It could be different with a different compiler, or even with the same compiler, but on a different platform or a different version of the compiler.
    Last edited by Elkvis; 11-17-2014 at 01:00 PM.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As an aside, if you want to pass a possible infinite amount of arguments to a function in C++, consider using variadic templates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > foo_func(va_arg(args, int), va_arg(args, int));
    Doing something like this is pure undefined behaviour.

    It makes no more sense than doing
    foo_func( x++, ++x );

    Your myfunc() did it properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Variadic Macro Function
    By KoRn KloWn in forum C Programming
    Replies: 3
    Last Post: 09-13-2013, 11:07 AM
  2. Replies: 8
    Last Post: 07-18-2012, 08:53 PM
  3. How to use variadic functions
    By Richardcavell in forum C Programming
    Replies: 1
    Last Post: 03-26-2011, 06:38 AM
  4. Variadic function
    By Richardcavell in forum C Programming
    Replies: 17
    Last Post: 03-01-2011, 01:34 AM
  5. Variadic Functions
    By coder8137 in forum C Programming
    Replies: 9
    Last Post: 01-12-2007, 08:25 PM