简体   繁体   中英

My C++ program for SPOJ generating primes gives expected output but crashes at the end. Please, let me know why it is happening

I know that the code is not an optimized one. I just wanted to practice segmented sieve of eratosthenes and finding prime numbers in a different way.

My compiler is Dev-C++ 5.11.
System specifications:
OS: Windows 8.1 Embedded Pro
Processor: Intel i5(3rd Generation)
RAM: 4Gb I don't know if the problem is due to hardware compatibility, I haven't use any low level procedures though.

Please explain to me why the program crashes.

在此处输入图片说明


#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#define NUM 1000000
using namespace std;
bool a[NUM]={0};
struct prime10
{
    vector<long long int> primes;
    int max;
    prime10()
    {
        max=NUM;
    }
}p;
void preprocess();
bool ifprime(unsigned long long int n);
void primemn(int m,int n);
int main()
{
    preprocess();
    int t=0;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        primemn(a,b);
    }
    return 0;
}
void preprocess()
{
    for (int i=2; i<=NUM; i++){
    if (a[i]==0){
        for (int j = i*2; j<=NUM; j+=i){
                a[j]=1;
            }
        }
    }
}
bool ifprime(unsigned long long int n)
{
    if(n<NUM){
        if(!a[n])
        return true;
    }
    else if(n%2==0)
    return false;
    else
    {
        for(int i=3;i<sqrt((long double)n);i+=2)
        {
            if(ifprime(i))
            {
                if(n%i==0)
                return false;
            }
        }
    }
    return true;    
}
void primemn(int m,int n)
{
    if(m<NUM&&n<NUM)
    {
        for(long long int i=m;i<=n;i++)
        {
            if(i==0||i==1)
            {
            }
            else if(!a[i])
            printf("%lld\n",i);
        }
    }
    else if(m<NUM&&n>NUM)
    {
        if(n<=p.max)
        {
            for(long long int i=m;i<NUM;i++)
            {
                if(!a[i])
                printf("%lld\n",i);
            }
            for(int i=0;i<p.primes.size();i++)
            printf("%lld\n",p.primes[i]);
        }
        else if(n>p.max)
        {
            for(long long int i=m;i<NUM;i++)
            {
                if(!a[i])
                printf("%lld\n",i);
            }
            for(int i=0;i<p.primes.size();i++)
            printf("%lld\n",p.primes[i]);
            for(int k=(p.max)+1;k<=n;k++)
            if(ifprime(k)){
            printf("%lld\n",k);
            p.primes.push_back(k);
            p.max=k;
            sort(p.primes.begin(),p.primes.end());
            }
        }
    }
    else if(m>NUM&&n>NUM)
    {
        if(m<=p.max&&n<=p.max)
        for(int o=0;o<p.primes.size();o++)
        {
            if(p.primes[o]>=m&&p.primes[o]<=n)
            printf("%lld\n",o);
        }
        else if(m<=p.max&&n>p.max)
        {
            for(int o=0;o<p.primes.size();o++)
            {
                if(p.primes[o]>=m&&p.primes[o]<=n)
                printf("%lld\n",o);
            }
            for(int o1=(p.max)+1;o1<=n;o1++)
            if(ifprime(o1)){
            printf("%lld\n",o1);
            p.primes.push_back(o1);
            p.max=o1;
            sort(p.primes.begin(),p.primes.end());
            }
        }
        else if(m>p.max&&n>p.max)
        {
            for(int o1=m;o1<=n;o1++)
            if(ifprime(o1)){
            printf("%lld\n",o1);
            p.primes.push_back(o1);
            p.max=o1;
            sort(p.primes.begin(),p.primes.end());
            }
        }   
    }
}

You're writing outside the boundary of a - your preprocess loops should be < NUM .

Overstepping this boundary has undefined behaviour.

It may cause corruption of the neighbouring p , and as p 's first member is a vector , a crash when the vector is destroyed is very likely.

Of course other scenarios are possible, but it sounds like this is what's happening in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM