Microsoft KB Archive/66738

= PRB: Unpredictable Results When >1 va_arg in Parameter List =

Article ID: 66738

Article Last Modified on 7/5/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft C Professional Development System 6.0a

 Microsoft C Professional Development System 6.0a

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q66738

<div class="notice_section">

<div class="symptoms_section">

SYMPTOMS
When the va_arg macro is used more than once in a function parameter list, an application produces different results depending on the version of the C compiler used to build the application.

<div class="cause_section">

CAUSE
The C standard does not precisely define expression evaluation order. In many cases, the compiler implementation determines the order in which an application evaluates expressions.

<div class="resolution_section">

RESOLUTION
To preserve the original order of the arguments, use the va_arg macro only once in a given expression. One method is to evaluate the expression in a loop that processes each variable argument and stops when the last parameter is reached.

<div class="moreinformation_section">

MORE INFORMATION
The va_arg macro provides a method to access the arguments of a function when the function can accept a variable number of arguments. The macro returns a variable parameter by incrementing the value of the arg_ptr that points to the list of arguments. The values returned depend on the order in which the application evaluates the va_arg macros.

However, because the C language does not guarantee the order in which most expressions are evaluated, application code should not depend on a particular evaluation order. The sample code below demonstrates this behavior and the unwanted side effects that can occur when code that depends on evaluation order is compiled with different versions of the same compiler.

Sample Code #1

 * 1) include <stdio.h>
 * 2) include <stdarg.h>

void myfunc(int, ...);

void main(void) {  myfunc(1, 2, 3, 4); }

void myfunc(int first, ...) {  va_list arg_ptr; va_start(arg_ptr, first);

printf(&quot;%d %d %d %d\n&quot;, first, va_arg(arg_ptr, int),                                 va_arg(arg_ptr, int),                                  va_arg(arg_ptr, int)); } When this sample is compiled with C version 5.1 or C/C++ version 8.0 for MS- DOS, the sample produces the following output: <pre class="fixed_text">  1 4 3 2 However, when this sample is compiled with Microsoft C versions 6.0, 6.0a, 6.0ax, Microsoft C/C++ version 7.0, or Microsoft Visual C++ 32-bit Edition, versions 1.0 through 4.2, the sample produces the following output: <pre class="fixed_text">  1 4 4 4 In Microsoft Visual C++ versions 5.0 and 6.0, the output is the same as C 5.1 and C/C++ 8.0.

In the following sample code, the expressions are evaluated correctly without regard to the expression evaluation order.

Sample Code #2

 * 1) include <stdio.h>
 * 2) include <stdarg.h>

void myfunc(int, ...);

void main(void) {  myfunc(1, 2, 3, 4, -1);     /* -1 is a flag */ }

void myfunc(int first, ...) {  int temp; va_list arg_ptr; va_start(arg_ptr, first);

temp = first; while(temp != -1)       /* test for flag (-1) */ {     printf(&quot;%d &quot;, temp); temp = va_arg(arg_ptr, int); } } The output is as follows: <pre class="fixed_text">   1 2 3 4

Keywords: kbcrt kbprb KB66738

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.