std min error during optimization

Ian Lance Taylor iant@google.com
Sun Jul 26 07:57:00 GMT 2009


TRaj <tryitn1@gmail.com> writes:

> int g(int mask,int lvl){
>   assert(mask<(1<<N));
>
>    printf("MASK=%d\n",mask);
>
> 	if(gdo2[mask]) return gdo2[mask];
>
> 	if(lvl==N){
> 		if(done[mask]) return INF;
> 		 return mask;
> 	 }
>  	 int ans=INF,i;
> 	 if(done[mask]==0) ans=mask;
> 	for(i=0;i<N;i++)
> 		if((mask&(1<<i))){
> 			ans=min(ans,min(g(mask^(1<<i) ,lvl+1),g(mask,lvl+1)));
> 		}
>
> 	return gdo2[mask]=ans;
>
> }

In this line:

> 			ans=min(ans,min(g(mask^(1<<i) ,lvl+1),g(mask,lvl+1)));

the function g calls itself twice.  The order of those function calls is
undetermined and will be different depending upon the function is
compiled.  This is a problem since g acts differently based on global
variable state.

Ian



More information about the Libstdc++ mailing list