C++ 程序意外退出
c++ programm exiting unexpectedly
我有一个Programm,其中我有两个嵌套的for循环。外部迭代参数为 x,内部参数为 y。这段代码继续到 x:5 和 y:4,但我希望它能持续到 x:1000 和 y:999。
你能解释一下发生了什么吗?
所以我有这个代码:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
printf("start");
long double result;
for (unsigned int x = 0; x < 1000; x++) {
for (unsigned int y = 0; y < x; y++) {
result = pow(50, x) - pow(50, y);
if (((int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);
}
}
}
}
问题就在这里:
if (((int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);
更准确地说,在条件下
((int)result) == result // the same as (int)result == result
这仅将您的输出限制在x == 5
和y == 4
的点 — 循环本身继续x == 1000
和y == 999
。
如果在这种情况下将int
更改为long int
,您可能会看到它:
if (((long int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);
您的输出会更长(最多x == 11
和y == 10
在我的机器中(。
为了调试您的程序,我在"if"语句之前添加了以下"printf"
printf("x: %07d y:%07d %d %Lfrn", x, y, (int)result, result);
我可以看到以下结果:
...
x: 0000005 y:0000004 306250000 306250000.000000
x: 0000006 y:0000000 -2147483648 15624999999.000000
...
当您将长双精度转换为 in 和结果 != (int(result 时,您可以看到整数类型溢出,这就是为什么您的程序在 x:4 y:5 之后停止打印数据的原因
但是为什么需要"如果"语句呢?您要检查什么条件?也许你可以删除它?
编辑:因为,如前所述,我提出的答案非常不正确,所以请允许我修复它。
数据类型的最小值/最大值
我想首先参考代码,您可以在其中轻松显示系统上某些数据类型的最大值:
template<typename T>
void showMinMax() {
cout << "tmin: " << numeric_limits<T>::min() << endl;
cout << "tmax: " << numeric_limits<T>::max() << endl;
cout << endl;
}
并在您的main()
函数中
cout << "int: " << endl;
showMinMax<int>();
cout << "long int: " << endl;
showMinMax<long int>();
cout << "float: " << endl;
showMinMax<float>();
cout << "double: " << endl;
showMinMax<double>();
cout << "long double: " << endl;
showMinMax<long double>();
打印这些限制一次以进行调试,并牢记它们。
您的 if 子句
现在,@MarianD的评论非常正确:您(尝试(将结果保存在long double
变量中,但随后将其压缩回long int
(甚至int
(进行比较。这不会让你得到你最喜欢的效果。
看看if
-子句中的另一个调试打印语句:
if (((long int)result) == result && result!=0) {
// Compare the value for the long int with the max value for the long int data type
printf("Comparing:nt(int)result: %dnt(long int)result: %ldntresult: %Len",(int)result,(long int)result,result);
printf("x: %07d y:%07d rn", x, y);
}
在这里,您可以观察成功比较后比较的值。您会注意到,(long int)result
的最后一个值(在x==11
和y == 10
处(接近系统上long int
的最大值。
我不太明白你为什么要做这样的比较,但是当你在迭代中前进时,你会遇到其他麻烦:
pow(( 函数
正如您在pow()
函数的 C++ 引用中看到的那样,根据输入参数,它返回不同的输出参数。我添加了另一个调试语句,如果pow()
函数的结果Inf
,则中断循环:
long double ld50 = 50.0;
rx = pow(ld50,x);
if(isinf(rx)){
printf("pow(50,%d) = Infn",x); rx = pow(ld50,x-1);
printf("Last valid value pow(50,%d) = %Len",x-1,rx);
break;
}
另请参阅isinf()
函数的C++参考。
取决于您使用的C++标准,例如 C++98
波(双基数,双指数(;
浮点数 pow(浮点基数、浮点指数(;长双
基(长双基、长双指数(;
双 Pow (双基数,整数指数(;
长双基(长双基,整数指数(;
您必须改变输入参数的类型才能获得pow()
函数所需的输出参数。
建议的解决方案
#include <iostream>
#include <cmath>
using namespace std;
int main() {
printf("startn");
long double result, rx, ry;
long double ld50 = 50.0;
for (unsigned int x = 0; x < 1000; x++) {
// Computing the power apriori in long doubles
rx = pow(ld50,x);
// Checking for infinity, if true, break
if(isinf(rx)){
printf("pow(50,%d) = Infn",x); rx = pow(ld50,x-1);
printf("Last valid value pow(50,%d) = %Len",x-1,rx);
break;
}
for (unsigned int y = 0; y < x; y++) {
ry = pow(ld50,y);
result = rx-ry;
printf("x: %07d y:%07d res: %Lern", x, y,result);
}
}
}
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在C++中对T*类型执行std::move的意外行为
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何让LLDB在成功时退出,在失败时等待
- C++控制台应用程序阻止退出
- 程序在执行程序的其余部分之前退出
- 处理除以零会导致<csignal>意外行为
- 构造函数在退出函数时无法初始化一个参数
- 使用vscode调试时,GDB意外退出
- C++ 程序意外退出
- 使用海湾合作委员会进行消毒,导致意外提前退出
- Qt - 意外的 GDB 退出
- PHP7 和 IIS8 HTTP 错误 500.0 - FastCGI 进程意外退出
- Xcode 6 在我按下输出窗口后意外退出
- 在 g++ 中使用 -pass-exit-code 时出现意外的退出状态
- 在lxc容器上调试意外退出且没有错误的C++程序
- 如何监视意外退出的线程
- Visual Studio 2015+libtcod-意外退出