I'm having a problem with a seemingly simple piece of code. I created a new .net console application and inserted this code:
#include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() { int xyz=0; //broken int testar __gc[,]; testar=new int __gc[3,5]; for(int i=0;i<=testar->GetUpperBound(0);i++) { for(int j=0;j<=testar->GetUpperBound(1);j++) { testar[i,j]=xyz++; printf("xyz=%d, testar[%d,%d]=%d\n",xyz,i,j,testar[i,j]); } } //works int testar2 __gc[]; testar2=new int __gc[3]; for(int i=0;i<=testar2->GetUpperBound(0);i++) { testar2[ i ]=xyz++; printf("xyz=%d, testar2[%d]=%d\n",xyz,i,testar2[ i ]); } return 0; } |
When I compile and run, the output shows that xyz isn't being incremented in the first section, but is in the second. If I change the first section to
testar[i,j]=xyz;
xyz++;
it works.
Also, if I change the initialization of xyz to
xyz=3;
the results are even stranger- the first section shows xyz=3 everytime through the loop, but testar[i,j] is always 0.
Windows XP Pro, Microsoft Development Environment 2003 Version 7.1.3088
Microsoft .NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C++ .NET 69459-006-3783734-18930
I did a clean and a recompile and nothing changed.
Any ideas Thanks-
Sean

post increment problem
Alsin
Thanks for the reply, but that's not it. I understand the difference between post-increment and pre-increment. In the bad section, xyz is *never* incremented.
I did change the code to pre-increment, and the results are similar. The only difference I noticed was that if I initialize xyz to 3 and use pre-increment, all of the elements of testar[] are set to 3, whereas with post-increment they were set to 0. That's wrong in both cases.
Sean
StephenWong
I think it's the ++x and x++ problem,in first works,you can modify the code" testar[i,j]=xyz++; "to "testar[i,j] = ++xyz;",in this section, the xyz increase first and return the increased value, and in origin code,the return value isn't the increased value!
best regard
Kenneth Yang
MLR
I was able to reproduce the issues using the VC2003 tools by getting:
xyz=0, testar[0,0]=0
xyz=0, testar[0,1]=0
xyz=0, testar[0,2]=0
xyz=0, testar[0,3]=0
xyz=0, testar[0,4]=0
xyz=0, testar[1,0]=0
xyz=0, testar[1,1]=0
xyz=0, testar[1,2]=0
xyz=0, testar[1,3]=0
xyz=0, testar[1,4]=0
xyz=0, testar[2,0]=0
xyz=0, testar[2,1]=0
xyz=0, testar[2,2]=0
xyz=0, testar[2,3]=0
xyz=0, testar[2,4]=0
xyz=1, testar2[0]=0
xyz=2, testar2[1]=1
xyz=3, testar2[2]=2
It appeares that is an issue that has been fixed in VC2005 since using the VC2005 tools I get the following expected output:
xyz=1, testar[0,0]=0
xyz=2, testar[0,1]=1
xyz=3, testar[0,2]=2
xyz=4, testar[0,3]=3
xyz=5, testar[0,4]=4
xyz=6, testar[1,0]=5
xyz=7, testar[1,1]=6
xyz=8, testar[1,2]=7
xyz=9, testar[1,3]=8
xyz=10, testar[1,4]=9
xyz=11, testar[2,0]=10
xyz=12, testar[2,1]=11
xyz=13, testar[2,2]=12
xyz=14, testar[2,3]=13
xyz=15, testar[2,4]=14
xyz=16, testar2[0]=15
xyz=17, testar2[1]=16
xyz=18, testar2[2]=17
If you are indeed using VC2003, I believe a possbile workaround would be to break
testar[i,j]=xyz++; into the separate statments
testar[i,j]=xyz;
xyz++;
Hope this helps!
If the work around is not acceptable then feel free to contact PSS to request a fix for it.
Thanks,
Ayman Shoukry
VC++ Team