Getting SIGSEGV error on SPOJ but couldn’t find what is wrong

I want to find all prime numbers between two numbers.
Here is my code:

   #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>

int prim(int n)
{
    int s, i;
    if (n == 1 || n == 2)
        return 1;
    if (n % 2 == 0)             // no even numbers
        return 0;
    s = (int)sqrt(n);           // limit the loop
    for (i=3; i<=s; i+=2)       // odd numbers only
        if (n % i == 0)
            return 0;
    return 1;
}

void print(int a,int b)
{
    int *p,i,k;
    int g;
    g=(b/2);
    p = (int *) malloc (sizeof(int)*(b-a+1));
    for(i=0;i<(b-a+1);i++) p=a+i;
    for(i=0;i<=g;i++)
    {
        if(p && prim(p))
        {
            for(k=i*2+a;k<b-a;k+=i+a)
                p[k]=0;
        }
        if(!prim(p))
        {
            p=0;
            for(k=i*2+a;k<=b-a;k+=i+a)
                p[k]=0;
        }

    }
    for(i=0;i<(b-a+1);i++) if(p!=0) printf("%d ",p);
    free(p);
}

int main(void)
{
    int t,i,m,n;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d %d",&m,&n);
        print(m,n);
    }
    return 0;
}

I get the SIGSEGV error. SPOJ explanation is:
SIGSEGV (signal 11) – the most common error for non-interpreted languages: a “segmentation fault” of the program. This may be caused e.g. by an out-of-scope array index causing a buffer overflow, an incorrectly initialized pointer, etc.


Download getting.sigsegv.error.on.spoj.but.couldnt.find.what.is.wrong.zip
Direct Link


Download


Download getting.sigsegv.error.on.spoj.but.couldnt.find.what.is.wrong.zip
Mediafire


Download


Download getting.sigsegv.error.on.spoj.but.couldnt.find.what.is.wrong.zip
Sendspace


Download


Download getting.sigsegv.error.on.spoj.but.couldnt.find.what.is.wrong.zip
Keep2Share


Download


Download getting.sigsegv.error.on.spoj.but.couldnt.find.what.is.wrong.zip
Depositfiles


Download

Both comments and pings are currently closed.

Comments are closed.