为什么输出错误崩溃
Why does it output error crash?
我正在编写一个代码来分析游戏,我有问题。该代码经历了7个六面滴度(从1111111到666666(的每种组合,并且对于每个组合,如果组合有一些分数。因此,我试图检查它是否可以将每个骰子组合的每个骰子组合和相关的要点放在控制台中,直到1223353,但在1223354上,它"崩溃了",如图像所示,我们可以看到运行我的控制台运行我的控制台代码。所以我不明白为什么它只是在〜10'000迭代后停止。
P.S。:我尝试在代码块中运行它,并且它也不起作用...
谢谢您的回答!
好吧,当调试器到达此行时,它会崩溃:
if (sptopt[k]==sptopt[j]){ // here k == 3 and sptopt.size() == 3
sptopt.erase(sptopt.begin()+k); // this line crashes
sdesopt.erase(sdesopt.begin()+k);
soptch.erase(soptch.begin()+k);
}
我在崩溃时看过变量的状态,确实是:
k == 3和sptopt.size((== 3
基本上您是从向量循环和擦除(从内存的角度来看非常效率(,而不是检查k的值不等于/高于向量的大小。
这是我得到的堆栈:
Thread 1Queue : com.apple.main-thread (serial)
#0 0x00007fff95687f49 in _platform_memmove$VARIANT$Haswell ()
#1 0x000000010001b8ab in std::__1::enable_if<(is_same<std::__1::remove_const<int>::type, int>::value) && (is_trivially_copy_assignable<int>::value), int*>::type std::__1::__move<int, int>(int*, int*, int*) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1904
#2 0x000000010001b856 in int* std::__1::move<int*, int*>(int*, int*, int*) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1913
#3 0x000000010001b802 in std::__1::vector<int, std::__1::allocator<int> >::erase(std::__1::__wrap_iter<int const*>) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1676
#4 0x000000010001b675 in main at /Users/**/Documents/iosapps/Chat App/newdaf/newdaf/main.cpp:604
每个您即将访问循环中大小变化的向量,您必须添加检查,例如:
if ( ( k < sptopt.size() ) && ( j < sptopt.size()) &&
( k < sdesopt.size() ) && ( j < sdesopt.size()) &&
( k < soptch.size() ) && ( j < soptch.size()) ...
否则,您访问/删除一个不存在的元素。在删除的情况下,您会遇到运行时错误。仅访问时,C 可能并不像告诉您您不是自己想的地方(它确实输入了k的功能,k是无效的索引(,而从那时起,您的程序的行为非常奇怪。
因此,您需要查看您的功能,并确保变量永远不会超过所使用的向量的尺寸。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- cv::namedWindow() 崩溃,分段错误
- 由于指针算法错误,代码在 memcpy 中崩溃
- python-clang 崩溃并出现分段错误
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- C++如果调用 vector,矢量分割错误会导致崩溃
- libcurl curl_easy_perform崩溃(分段错误)c++
- C++ rezing 动态数组最多可以处理一定数量的元素,但在某些时候会崩溃并显示错误 (0XC0000005)
- 从转码示例中使用时,比例过滤器会带有错误崩溃
- 在函数内部错误定位后,从函数中释放返回的指针会导致芯片崩溃
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- React 本机 0.59 应用程序崩溃并出现 libc 错误
- 为什么输出错误崩溃
- C 时间类没有编译错误,执行停止和应用程序在CheckDay函数中崩溃错误
- 进程"mingw32-make.exe"崩溃错误
- 崩溃错误:矢量下标超出范围