Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

The following bug exists in the Microsoft VS 2010 cpp compiler.
C++
void func1( int a, int b, int c... )
{
  va_list ap;
  va_start( ap, b );
  //some code here
  va_end(ap);
}


Microsoft has improved optimizations and now parameter b(exactly speaking, the 2nd parameter before ... is passed in ecx register. there is no problem with this in almost all cases, but in the example above, the instruction va_start( ap, b ) can not take address of ecx, and generates wrong code. It is expected that the compiler should generate an error once it can not take the address of a register parameter.
I put this post here because I could not connect to microsoft, due to sanctions on the country I live in, pardon in progress
mr.abzadeh

Edit: This code worked correctly until a weak ago, and then I noticed the wrong compilation and changed my code. I have not changed my project settings. I use visual studio 2010 ultimate SP1, and the problem occurs in releasemode, optimizations on.
Posted
v3
Comments
Matthew Faithfull 16-Apr-13 6:15am    
This is interesting although not a question.
Don't be surprised if this gets downvoted and removed because it's not a question.
You cold post in 'Wierd & Wonderful' CP forum if this happens or rephase this as a question.
One more suggestion: va_list and friends are actually MACROS. It would be good to be able to see a second version of the above code with the MACROS expanded so that we can see what the compiler actually has to work on.

What surprises me in your code, is that you are not using parameter c, but b to start the scan of optional parameters. The documentation on va_start states:
C++
void va_start(
   va_list arg_ptr,
      prev_param 
); // (ANSI version)

va_start sets arg_ptr to the first optional argument in the list of arguments passed to the function. The argument arg_ptr must have va_list type. The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro's behavior is undefined. va_start must be used before va_arg is used for the first time.

So, in opinion you are building here on a non-documented feature.
 
Share this answer
 
Comments
mr.abzadeh 16-Apr-13 6:59am    
Almost all programmers use va_start to set arg_ptr to the first optional argument, but va_start is a macro, and nobody nowhere has restricted it's use to point to another parameter other than the first. My code has 10 parameters, following by ..., and The 10th parameter was there for clarity and readablity. I used va_start to point to 9th parameter
nv3 16-Apr-13 9:37am    
All uses of va_start I have seen so far used the last mandatory parameter as prev_param argument. And that's also what the documentation says. If you use it in a different way, you are on your own.
As nv3 points out, you are not using the macro correctly. If you use it any any way other than as described in the documentation, then all bets are off, and you cannot complain if it does not do what you think it should. If you believe that is incorrect then you should be talking to Microsoft about it rather than posting here.
 
Share this answer
 
The following code
C
#include<stdio.h>
#include <stdarg.h>
void func1( int a, int b, int c,... )
{
	int n, k;
  va_list ap;
  va_start( ap, b );
  //some code here
	printf(" a=%d, b=%d, c=%d, other:\n", a,b,c);
	for (n=0; n<c; n++)
	{
		k = va_arg(ap, int);
		printf("%d\n", k);
	}
  va_end(ap);
}
int main()
{
	int a=5;
	func1(a,5,3,2,3,4); 
}


produces the expected output on my Visual C++ 2010 Express Edition.
 
Share this answer
 
Comments
Matthew Faithfull 16-Apr-13 6:24am    
Which reminds me that the OP and anyone trying to replicate this should post your Project Settings :-) , not just the Optimization ones but the full command line as Exception settings, RTC and even language extensions have the capacity to alter the outcome of this if it is an uncontrolled case.
mr.abzadeh 16-Apr-13 6:26am    
This hapens in release mode, optimizations on, visual studio SP1.
I had a code similar to this in my application, and It worked correctly until the last weak.
But now it does not. I think my visual studio SP1 optimization has been updated thru Internet, and the problem occured
mr.abzadeh 16-Apr-13 6:32am    
Matthew has posted a more exact comment, (project settings, ...) but I have not changed anything in my 10 applications projects, and the I my code was compiled correctly until a weak ago. what may have been happened?
I will try to find the code that va_start is expanded to in this case

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900