while 循环 c++ 中的非法指令
Illegal instruction in while loop c++
我被指派编写一个简化有理数的程序。我想做的是计算 gcd,然后将数字除以 gcd。但是程序返回一个非常奇怪的错误。
代码:
void read_rational(int& num, int& den) {
char bar;
if (cin >> num >> bar >> den) {
cout << "hi";
int a = num;
int b = den;
while (b != 0) {
int r = a%b;
a = b;
b = r;
}
num /= b;
den /= b;
}
}
INPUT: 10/2 OUTPUT: Illegal instruction (core dumped)
INPUT: 90/8 OUTPUT: Illegal instruction (core dumped)
我尝试注释掉脚本的某些部分。程序似乎仅在存在 while 循环时才崩溃。但我看不出它有什么问题。
事实上,问题是while
循环。完成后,b
实际上是 0,因此之后的除法会引发这些错误。 我认为你想要的是a
而不是b
.
相关文章:
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++中的移动分配出现问题.非法指令: 4.
- while 循环 c++ 中的非法指令
- 来自 VS C++ 在 Windows 上的非法指令
- AVX512 非法指令
- 在Visual "Microsoft studio 2019"上设置OpenCV 4.1.1时遇到问题?(非法指令。
- 在运行基本 Avx512 代码时获取非法指令
- exe_common.inl中的非法指令
- 从C 调用Tensorflow Lite .tflite CNN模型时,非法指令
- _mm_fmadd_pd程序收到信号SIGILL,非法指令
- 仅在64位释放模式中的位移位非法指令
- 程序收到信号Sigill非法指令
- 标准::字符串中的非法指令
- 获取列表的第一个和最后一个元素<string>给我非法指令错误
- 为什么Folloing代码在2010年Visual Studio(X64应用程序)中抛出非法指令例外
- std::p romise::get_future 提出非法指令 (SIGILL)
- 使用mpopcnt编译会导致非法指令错误
- 使用带有自定义对齐分配器实现的最新g++,使用SSE和-O3选项编译时出现非法指令(核心转储)
- 程序接收到信号SIGILL,非法指令
- 在VMWare机器上编译的程序在亚马逊服务器上运行时会因非法指令而崩溃