命令在调用方法时终止

Command Terminated on calling a method

本文关键字:终止 方法 调用 命令      更新时间:2023-10-16

调用此方法时:

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)后的某个时间刷新它们。