I am trying to solve the PRIME1 problem of SPOJ using Sieve of Eratosthenes. The code works fine for lower integers but shows the following error for long ints. I am still getting an error for large numbers. Example - code runs fine for m = 100000000 and n = 110000000 but shows the following error for m = 999899999 and n = 999999999. The error is - "Unhandled exception at 0x778a15ee in spoj1.exe: 0xC0000005: Access violation writing location 0x00000000." Similar error is shown by SPOJ - "SIGSEGV" error. Please help. Also, I am new to coding so please bear with any egregious mistakes.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, test, i, k;
long int *arr, p;
scanf("%d", &test);
while (test--)
{
scanf("%d%d", &m, &n);
arr = (long int *)calloc(n - 1, sizeof(long int));
if (m == 1)
{
m = 2;
}
arr[0] = 2;
for (i = 1; i < n - 1; i++)
{
arr[i] = arr[i - 1] + 1;
// printf("%d\n",arr[i]);
}
for (i = 0; i < n - 1; i++)
{
if (arr[i] != 0)
{
for(k = arr[i]-2; k<n-1;)
{
k = k + arr[i];
if(k < n-1)
{
arr[k] = 0;
}
}
}
for (i = 0; i < n - 1; i++)
{
if (arr[i] != 0 && arr[i] >= m)
{
printf("%d\n", arr[i]);
}
}
printf("\n");
}
free(arr);
return 0;
}