积极答复者
a debug question between VS2008 and VS2010

问题
-
Today I program the following C++ codes and meet a problem when debugging in VS2010.
The codes are:
#include<iostream>
#include<string>
using namespace std;
bool check(int i,int j,int x,int y,string str1,string str2);
void main()
{
int k=0;
int count=0;
bool m=true;
string str1="abc";
string str2="abcabab";
int L1=str1.length ();
int L2=str2.length ();
for(int i=k;i<L2;i++)
{
if(str1[0]==str2[i])
{ k=i;
if(str1[L1-1]==str2[k+L1-1])
m=check(0,L1-1,k,k+L1-1,str1,str2);
else m=false;
if(m==true)
count++;
k++;
}
}
cout<<count<<endl;
}
bool check(int i,int j,int x,int y,string str1,string str2)
{
while(i<j)
{
if(str1[(i+j)/2]==str2[(x+y)/2])
{ return true;
check(i,(i+j)/2,x,(x+y)/2,str1,str2);
check((i+j)/2,j,(x+y)/2,y,str1,str2);
}
else
return false;
}
}
When I run it in VS2010, it shows a error messages about "string subscript out of range" in line1441 of include\xstring.
However, everything is OK when I run the same codes in VS2008 and it shows the result: 1.
I have no idea what makes it different between vs2008 and vs2010.
Could help me to solve this codes? Thanks.
答案
-
代码中有明显的下标访问越界的情况,VS2010能正确指出来是好事。
另:请最好用中文提问。
- 已标记为答案 mengtian2011 2011年3月3日 12:02
- 取消答案标记 mengtian2011 2011年3月3日 13:51
- 已标记为答案 lucy-liuModerator 2011年3月4日 9:40
-
-
代码中有明显的下标访问越界的情况,VS2010能正确指出来是好事。
另:请最好用中文提问。
“下标访问越界”的情况出现在:正如Igor Tandetnik在英文论坛所说的那样“For large enough values of k, k+L1-1 is beyond the size of str2.”
- 已标记为答案 lucy-liuModerator 2011年3月4日 9:40
全部回复
-
Hi mengtian,
This is a chinese forum , please ask question in chinese. Or you can ask question in english forum http://social.msdn.microsoft.com/Forums/en-US/category/visualc .
Thank you for your understanding,
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
您好,这是中文论坛,请您用中文来提问,如果您想用英文提问请到c++的英文论坛发帖 :http://social.msdn.microsoft.com/Forums/en-US/category/visualc 。
谢谢您的理解!
Best regards,
Lucy
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
代码中有明显的下标访问越界的情况,VS2010能正确指出来是好事。
另:请最好用中文提问。
- 已标记为答案 mengtian2011 2011年3月3日 12:02
- 取消答案标记 mengtian2011 2011年3月3日 13:51
- 已标记为答案 lucy-liuModerator 2011年3月4日 9:40
-
-
代码中有明显的下标访问越界的情况,VS2010能正确指出来是好事。
另:请最好用中文提问。
“下标访问越界”的情况出现在:正如Igor Tandetnik在英文论坛所说的那样“For large enough values of k, k+L1-1 is beyond the size of str2.”
- 已标记为答案 lucy-liuModerator 2011年3月4日 9:40