Home > Net >  How to stop crashes in a recursion loop
How to stop crashes in a recursion loop

Time:10-26

I made this simple function, but it crashes at p=29. It makes a stopped working error window. Please Help Me

#include <iostream>
using namespace std;

char *primality(unsigned long,unsigned long i=0);

int main()
{
    for(int i=0;i<1000;i  )
        cout<<i<<": "<<primality(i)<<endl;
};

char *primality(unsigned long p,unsigned long i)
{
    if(i==0)
    {
        if(p<=1)
            return "NEITHER PRIME NOR COMPOSITE";
        else if(p==2||p==3)
            return "\tPRIME";
        else if(p%2==0||p%3==0)
            return "\tCOMPOSITE";
    }
    i=5;
    if(i*i<=p)
        if(p%i==0||p%(i 2)==0)
            return "\tCOMPOSITE";
        else
            return primality(p,i 6);
    else
        return "\tPRIME";
}

The output after 29 is stopped and causes a crash Output:

0: NEITHER PRIME NOR COMPOSITE
1: NEITHER PRIME NOR COMPOSITE
2:      PRIME
3:      PRIME
4:      COMPOSITE
5:      PRIME
6:      COMPOSITE
7:      PRIME
8:      COMPOSITE
9:      COMPOSITE
10:     COMPOSITE
11:     PRIME
12:     COMPOSITE
13:     PRIME
14:     COMPOSITE
15:     COMPOSITE
16:     COMPOSITE
17:     PRIME
18:     COMPOSITE
19:     PRIME
20:     COMPOSITE
21:     COMPOSITE
22:     COMPOSITE
23:     PRIME
24:     COMPOSITE
25:     COMPOSITE
26:     COMPOSITE
27:     COMPOSITE
28:     COMPOSITE
29:
--------------------------------
Process exited after 2.855 seconds with return value 3221225725
Press any key to continue . . .

Also, note that I got a return value. Please tell the meaning of this error message

I'm also instructed to use char arrays instead of strings.

CodePudding user response:

29 is the first number, for which you enter into an endless recursion. As a consequence, the application crashes eventually.

I did not look at your algorithm in much detail, but I assume that i=5 is not really working as expected.

CodePudding user response:

In your primality() function,


char *primality(unsigned long p,unsigned long i)
{
    if(i==0)
    {
        if(p<=1)
            return "NEITHER PRIME NOR COMPOSITE";
        else if(p==2||p==3)
            return "\tPRIME";
        else if(p%2==0||p%3==0)
            return "\tCOMPOSITE";
    }
    i=5;
    if(i*i<=p)
        if(p%i==0||p%(i 2)==0)
            return "\tCOMPOSITE";
        else
            return primality(p,i 6);
    else
        return "\tPRIME";
}

You're assigning the value 5 to i every time primality() runs. So the recursion has no stop. Eventually, your stack is too tired to hold all the different primality() calls, it will cause a stack overflow.

Change that to:

char *primality(unsigned long p,unsigned long i = 5);

char *primality(unsigned long p,unsigned long i)
{
    if(p<=1)
        return "NEITHER PRIME NOR COMPOSITE";
    else if(p==2||p==3)
        return "\tPRIME";
    else if(p%2==0||p%3==0)
        return "\tCOMPOSITE";

    if(i*i<=p)
        if(p%i==0||p%(i 2)==0)
            return "\tCOMPOSITE";
        else
            return primality(p,i 6);
    else
        return "\tPRIME";
}
  •  Tags:  
  • c
  • Related