命令在调用方法时终止
Command Terminated on calling a method
调用此方法时:
vector<int> primeFactors(int n)
{
printf("got this far");
vector<int> result;
for(int i = 0; i < nPrimes; i++)
{
printf("%d %d", nPrimes, primes[i]);
if(n % i == 0)
{
printf("i = %d", i);
n /= i;
result.push_back(primes[i]);
i = 0;
}
}
return result;
}
我在没有打印got this far
的情况下收到命令终止
我称之为主要方法:
int main()
{
int n;
scanf("%d", &n);
primeFactors(n);
return 0;
}
如有任何帮助,我们将不胜感激。
更新:
所以问题是,我最终进入了一个由g++检查的中野循环,导致它终止。无限循环是由几个错误引起的:基本上使用i
而不是primes[i]
,这是完全正确的代码:
vector<int> primeFactors(int n)
{
vector<int> result;
for(int i = 0; i < nPrimes && n > 1; i++)
{
if(n % primes[i] == 0)
{
n /= primes[i];
result.push_back(primes[i]);
i = -1;
}
}
return result;
}
我认为您的代码中有一个错误:
调用此函数时:
vector<int> primeFactors(int n)
{
printf("got this far");
vector<int> result;
for(int i = 0; i < nPrimes; i++)
{
printf("%d %d", nPrimes, primes[i]);
if(n % i == 0)
{
printf("i = %d", i);
n /= i;
这里您的i
是0,因此它将导致由除以零引起的崩溃。没有printf
输出的原因是stdout
缓冲区未刷新。在printf()
调用后使用flush()
刷新缓冲区或在可打印字符串中插入新行字符:
printf("got this farn");
p.S.I/O操作被缓冲。操作系统会在某个时间或显式询问(sync/fsync/flash)后的某个时间刷新它们。
相关文章:
- 等待 qthread 终止的正确方法是什么?
- 初始化和终止Qt应用程序的正确方法?
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 是否有一种安全的方法可以断言字符串视图是否以 null 终止?
- 使用信号检测子进程何时终止的最佳方法是什么?
- 一定时间后强制终止方法
- C :终止循环的最优雅方法,该循环在字符串向量上迭代
- 判断哪些线程已终止的有效方法
- 称为终止的纯虚拟方法
- 是否有一种方法可以在输入负数时平均不包括负数,这是您终止程序的方式
- 无法在线程运行方法中运行CryptoPP FileSink的情况下终止Qt线程
- 当 Execute() 在 C++ Builder 中终止时,不会调用 OnTerminate() 方法
- 在 Linux 中监视和终止其他程序停滞进程的更好方法?
- 旧版 Win32 DLL 中的空终止字符串 C++ - 有没有更好的方法
- 使用 Windows 版C++加速终止套接字 WinHttp API 的方法
- 命令在调用方法时终止
- 在字符串附加函数中,null终止无符号字符缓冲区的有效方法
- 当c#程序试图在c++ dll方法中使用cudaMalloc分配超过2GB时终止
- 在x秒后终止正在运行的线程的最佳方法是什么?
- Eclipse CDT——是否有一种方法可以在启动新启动之前终止之前正在运行的启动?