Home > Enterprise >  it crash on 6488 ! exit code is code=3221225725
it crash on 6488 ! exit code is code=3221225725

Time:03-19

i try to code a program which has to find prime numbers from 5 to a maximum number , the biggest number it can find is 64927 , and it is a prime number ( i think it has the 6489th place in prime numbers) . when i set max_list_size to 6488 , programm work normally and exit with 0, but for grater number it crash and exit whit code=3221225725 ( a bug in the executed software that causes stack overflow, leading to abnormal termination of the software) , can you help me guys ?

#include<stdlib.h>
#include<math.h>
long to_which_index ();
void is_prime();
void pluse_two();
void write_to_arrays();
long* prime_numbers;
long current_number = 5;
long next_prime_number = 0;
void terminate ();
long max_list_size = 6488;
int main()
{
prime_numbers = (long*)calloc(max_list_size,sizeof(long));
prime_numbers[0] = 3;

is_prime(current_number);

    
    
}
//is prime 
void is_prime()
{
    int to_which = to_which_index();
    for (long i = 0; i <= to_which; i  )
    {
        //(current_number % prime_numbers[i] == 0) ? pluse_two() : continue ;
        if((current_number % prime_numbers[i]) == 0)
        {
            pluse_two();
        }
    }
    write_to_arrays();
    
}

// pluse_2
void pluse_two()
{
    current_number = current_number  2;
    is_prime();
}

//write
void write_to_arrays()
{
    FILE* fptr;
    fptr = fopen("G:/Code/files_to_accses/prime_numbers.txt","a");
    next_prime_number   ;
    prime_numbers[next_prime_number] = current_number;
    fprintf(fptr,"%i\n",current_number);
    fclose(fptr);
    terminate();
}

//terminate programm
void terminate ()
{
    if (next_prime_number ==(max_list_size - 1))
    {
        free(prime_numbers);
        exit(0);
    }
    pluse_two();

}

//from index 0 to which index whe should check?
long to_which_index ()
{
    for (long i = 0; i < (next_prime_number 1); i  )
    {
        //( prime_numbers[i] < ceil(sqrt(double(current_number))) ) ? continue: return i;
        if( prime_numbers[i] < ceil(sqrt(current_number)) )
        {
            continue;
        }
        else{
            return i;
        }
    }
    
}```

CodePudding user response:

You have run out of stack becuase you recurse too deep, finding primes is not reall a good use of recursion

Exception thrown at 0x00007FFAAC6E341C (ntdll.dll) in ConsoleApplication3.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000003902E03FF8).

enter image description here

For me it died when current_number == 2801

Also this function

long to_which_index()
{
    for (long i = 0; i < (next_prime_number   1); i  )
    {
        //( prime_numbers[i] < ceil(sqrt(double(current_number))) ) ? continue: return i;
        if (prime_numbers[i] < ceil(sqrt(current_number)))
        {
            continue;
        }
        else {
            return i;
        }
    }

}

doesnt return a value all the time (thats not the casue tho)


Here is your recursion

//is prime 
void is_prime()   <<<<===================================|
{                                                        | 
    int to_which = to_which_index();                     |
    for (long i = 0; i <= to_which; i  )                 |
    {                                                    |
        //(current_number % prime_numbers[i] == 0) ? pluse_two() : continue ;                                                   |
        if ((current_number % prime_numbers[i]) == 0)    |
        {                                                |
            pluse_two();========================|        |
        }                                       |        |
    }                                           |        |
    write_to_arrays();                          |        |
                                                |        |
}                                               |        |
                                                |        |
// pluse_2                                      |        | 
void pluse_two() <<<<<<======================== |        |    
{                                                        |
    current_number = current_number   2;                 |
    is_prime(); ==========================================
}

you can see it plainly in the stack trace I added as a screen shot

Just change that plus_two function like this

void pluse_two()
{
    current_number = current_number   2;
  //  is_prime();
}
  •  Tags:  
  • c
  • Related