最普通的for循环产生了最让人头疼的问题

devil_may_care 2013-05-18 12:06:38
在下写了一个for循环,大约执行2000多次,但循环到 i=1660 的时候在循环中的一条语句上中断了,将i设置为1660拿出来单独测试那一句却又没问题,困扰了我好久,希望各位大神集思广益,指教一下小弟呀!
...全文
534 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码老猿 2013-05-20
  • 打赏
  • 举报
回复
有可能是其它地方的问题。写出界了。 你可以先试试这个方法. 依次停掉i=1 到 i=1659.那次程序工作了,那个地方就可能有问题。
赵4老师 2013-05-20
  • 打赏
  • 举报
回复
Setting a Breakpoint When a Variable Changes Value To set a breakpoint when a variable changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the name of the variable. 4. Click OK to set the breakpoint. Setting a Breakpoint When an Expression Changes Value To set a breakpoint when an expression changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression such as x+y. 4. Click OK to set the breakpoint. Setting a Breakpoint When an Expression Is True To set a breakpoint when an expression is true 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression, such as x==3, that evaluates to true or false. 4. Click OK to set the breakpoint. Setting a Breakpoint on a Variable Outside the Current Scope To break on a variable outside the current scope 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the variable name. 4. Select the drop-down arrow to the right of the text box. 5. From the menu that appears, click Advanced. The Advanced Breakpoint dialog box appears. 6. In the Expression text box, type the function name and (if necessary) the filename of the variable. 7. Click OK to close the Advanced Breakpoint dialog box. The information that you specified appears in the Expression text box in the Breakpoints dialog box. 8. In the Breakpoints dialog box, click OK to set the breakpoint. Note You can enter context information directly into the Expression field, using the advanced breakpoints syntax. For details, see Using Advanced Breakpoint Syntax. Setting a Breakpoint When the Initial Element of an Array Changes Value To break when the initial element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0], for example). 4. In the Number Of Elements text box on the Data tab, type 1. 5. Click OK to set the breakpoint on myArray [0]. Setting a Breakpoint When the Initial Element of an Array Has a Specific Value To break when the initial element of an array has a specific value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression containing the initial element of the array (myArray[0]==1, for example). 4. In the Number Of Elements text box, type 1. 5. Click OK to set the breakpoint on myArray [0]. Setting a Breakpoint When a Particular Element of an Array Changes Value To break when a particular element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the element of the array (myArray[12], for example). 4. In the Number Of Elements text box, type 1. 5. Click OK to set the breakpoint on myArray [12]. Setting a Breakpoint When Any Element of an Array Changes Value To break when any element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0]). 4. In the Number Of Elements text box, type the number of elements in the array. 5. Click OK to set the breakpoint on myArray. Setting a Breakpoint When Any of the First n Elements of an Array Change Value To break when any of the first n elements of an array change value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0], for example). 4. In the Number Of Elements text box, type n (for example, 10). 5. Click OK to set the breakpoint on myArray[0] through myArray[9]. Setting a Breakpoint When the Location Value of a Pointer Changes To break when the location value of a pointer changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the pointer variable name (p, for example). 4. Click OK to set the breakpoint. Setting a Breakpoint When the Value at a Location Pointed to Changes To break when the value at a location pointed to changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p or p->next, for example). 4. Click OK to set the breakpoint. Setting a Breakpoint When an Array Pointed to by a Pointer Changes To break when an array pointed to by a pointer changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p, for example). 4. In the Number Of Elements text box, type the length of the array in elements. For example, if the pointer is a pointer to double, and the array pointed to contains 100 values of type double, type 100. 5. Click OK to set the breakpoint. Setting a Breakpoint When the Value at a Specified Memory Address Changes To break when the value at a specified memory address changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the memory address for the byte. For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.) 4. In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords. 5. Click OK to set the breakpoint. Setting a Breakpoint When a Register Changes To break when a register changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type a register mnemonic, such as CS. 4. In the Number Of Elements text box, type the number of bytes to monitor. 5. Click OK to set the breakpoint. Setting a Breakpoint When a Register Expression Is True To break when a register expression is true 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression that contains a boolean comparison operator, such as CS==0. 4. In the Number Of Elements text box, type the number of bytes to monitor. 5. Click OK to set the breakpoint. Note When you set a data breakpoint, the debugger places the variable or variables used into a special debug register, if possible. The number of debug registers is limited. (Intel 80386 and later CPUs provide four debug registers. Motorola 680X0 and PowerPC chips have no debug registers.) Furthermore, stacked-based variables (parameters) cannot be placed into debug registers. If a breakpoint variable cannot be placed into a debug register, the debugger must examine the variable’s memory location after every instruction to determine whether the contents have changed. These extra memory accesses reduce execution speed of the program with the debugger. In some cases, the program may appear to hang. Performance may be especially slow if you are debugging a remote application.
赵4老师 2013-05-20
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
max_min_ 2013-05-20
  • 打赏
  • 举报
回复
错误找到了,结贴才是美德啊~!帅锅
devil_may_care 2013-05-20
  • 打赏
  • 举报
回复
问题解决了,多谢各位! 最后发现问题确实处在指针上面,因为有一个逻辑错误,只有在特定情况下才会出错,导致中间过程的指针数组为空,引用了空指针,导致了上面所说的错误! 多谢各位大神的好心帮忙,让我从中学到了好多查错、调试的方法!
cmh360 2013-05-20
  • 打赏
  • 举报
回复
肯定是自己搞得鬼 逻辑问题 一般都是访问越界
老寒猫 2013-05-19
  • 打赏
  • 举报
回复
一般这样的循环挂掉,大多的原因有如下两个,第一:for语句里面的逻辑处理语句或者函数出了问题;第二:就是你的数组越界了,因为数组在for循环过程中,或者别的地方(多线程)都会发生改变,需要注意。 拙见,望交流!
程志刚。。。 2013-05-19
  • 打赏
  • 举报
回复
肯定是指针问题
devil_may_care 2013-05-19
  • 打赏
  • 举报
回复
等大神啊,等大神哪!!!
devil_may_care 2013-05-19
  • 打赏
  • 举报
回复
代码我贴在空间,断点运行1660次的时候在IsGameover函数中调用的mobility函数的第一行(主函数.cpp第27行)停止,但正如我之前所说,单独拿出来测试第1660种情况用却有没问题 另外,那是一个二维向量,元素都是用push_back()函数插进去的,难道是内部指针出问题?
tonforce 2013-05-19
  • 打赏
  • 举报
回复
看看有没有越界访问 然后在for循环里下个条件断点,一步步走,看走到哪儿停下。
  • 打赏
  • 举报
回复
执行全部的循环,在1660次循环的时候,下个条件断点,看看上下文有没有什么问题。 不要轻易怀疑语言,很可能是自己使用不当。
代码老猿 2013-05-19
  • 打赏
  • 举报
回复
设个断点调试。 for(int i = 0; i < count; i ++) { if(i==1660){ int breakpoint = 0; /////// 断点 } MakeMove(gen.MoveList[0][i],board); cout<<i<<" "; IsGameOver(board); cout<<i<<" "; UnMakeMove(gen.MoveList[0][i],board); }
regainworld 2013-05-18
  • 打赏
  • 举报
回复
不好意思,望文生义了,看见gen.MoveList[0][1660],我就以为这个MoveList[0]下标最大是1659,再大就越界了
devil_may_care 2013-05-18
  • 打赏
  • 举报
回复
本想在博客中公开源码,但发现刚开通三天之内不能发文章
devil_may_care 2013-05-18
  • 打赏
  • 举报
回复
引用 9 楼 dahaiI0 的回复:
MakeMove。。差点堪称MakeLove了。
dahaiI0 2013-05-18
  • 打赏
  • 举报
回复
MakeMove。。差点堪称MakeLove了。
devil_may_care 2013-05-18
  • 打赏
  • 举报
回复
引用 7 楼 regainworld 的回复:
限制一下count不能超过1660
我测试过在第19行之后用for循环将所有走法输出,没有问题,可以完整输出所有的走法
regainworld 2013-05-18
  • 打赏
  • 举报
回复
限制一下count不能超过1660
devil_may_care 2013-05-18
  • 打赏
  • 举报
回复
我邮箱 devil_may_care@126.com 因为我们要参加一个比赛,代码不能完全公开,不好意思,若各位有意,给我发个邮件,我可以将所有代码发过去
加载更多回复(8)

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧