Thread: Unexpected behavior of stdarg

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    Unexpected behavior of stdarg

    Here's a small test program that demonstrates what I'm experiencing. I'm building it with gcc 4.2.1.

    Code:
    #include <stdarg.h>                                                 
    #include <stdlib.h>                                                 
    #include <stdio.h>                                                  
                                                                        
    void test(int foo, ...) {                                           
            va_list args;                                               
            va_start(args, foo);                                        
            printf("%lld\n", va_arg(args, long long));                  
            va_end(args);                                               
    }                                                                   
                                                                        
    int main() {                                                        
            // I would expect this to output -1                         
            test(0, -1);                                                
                      
            // Works with a cast                                         
            test(0, (long long)-1);                                     
    
            return 0;                                                   
    }
    The output of this is:

    4294967295
    -1

    The first number corresponds to (unsigned)-1. I cannot figure out why this conversion is being made. My understanding (and probably incorrect) is that the compiler should promote the untyped value of -1 to the type specified in va_arg.

    Can anyone shed light on why this is happening?

    Thanks,

    Fred

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > My understanding (and probably incorrect) is that the compiler should promote the untyped value of -1 to the type specified in va_arg.
    The compiler has no way of knowing you're going to use long long. It does NOT parse your entire function (the function might not even be available to look at).

    Consider things like scanf/printf - how are those parameters checked? How often have you tried something like
    printf("%d\n", "hello world");
    and got no warning and garbage results?
    Now you're fortunate enough to be using GCC, so you can get some error checking by using -Wformat.

    As soon as the compiler starts matching the ... as the parameters, then all the implicit type conversions offered by function prototypes go away and you're left with the default promotion rules.
    25.3 Special Issues with Varargs Functions
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.9
    That's why it's *WRONG* to pass 0 as NULL pointer to vararg function
    Even though you can pass 0 as NULL pointer to function with fixed args without any problem.
    IMO, you can simply just try printf("%f\n", 3);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. unexpected exception handler
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2008, 05:49 AM
  3. Unexpected results from function
    By ajdspud in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2005, 04:19 PM
  4. unexpected 'class Window (' ??? i dont understand the error
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 10:12 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM