0

this error keeps on appearing for every program i try to submit on spoj.pl

In the given code i need to find prime numbers between m - n for t no. of test cases . problem statement:http://www.spoj.com/problems/PRIME1/ the same error is appearing .. can anyone plss tell me why this error shows up again nd again .. here's my code

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

int main()

{
  int t;
  scanf("%d",&t);
  int *m,*n;
  m=(int *)malloc(sizeof(int)*t);
  n=(int *)malloc(sizeof(int)*t);
  int i=0;
  while(i<t)
     {
          scanf("%d%d",(m+i),(n+i));
          i++;
      }
  i=0;
  while(i<t)
    {
      long long int *list,j,k;
      list=((long long int*)malloc(sizeof(long long int)*(n[i]+1)));
      list[0]=list[1]=0;
      for(j=2;j<=*(n+i);j++)
           {
               *(list+j)=1;
           }
      float l=sqrt(*(n+i)+1);
      //int l=sqrt(*(n+i)+1);

      for(j=2;j<=l;j++)
           {
               if(*(list+j)==1)
                   {
                       //printf("\n%ld",j);
                       for(k=j*j;k<=*(n+i);k=k+j)
                           *(list+k)=0;
                    }
            }
      for(j=m[i];j<=n[i];j++)
           {
               if(*(list+j)==1)
                   {
                       printf("\n%ld",j);
                    }
            }
      printf("\n");
      free(list);
      i++;
 }
free(m);
free(n);  
return 0;
}
  • 1
    I think `list=((long long int*)malloc(sizeof(long long int)*(n[i]+1)));` you're allocating too much memory. I'm not sure whether that makes `malloc` return `NULL` or whether the programme is OOM-killed by the system. – Daniel Fischer Apr 05 '13 at 19:02
  • I can't find any error in your code accept you should have a warning for printf() using `%ld` where `j` is `long long int` use `lld` – Grijesh Chauhan Apr 05 '13 at 19:04
  • tried long int instead of long long int .. still not working – Abhinav Gupta Apr 05 '13 at 19:13

2 Answers2

2

First -- you should not cast malloc -- it can cause unexpected errors.

Second, there's no validation that you allocated the memory you need. There are three different places you're asking for memory and never look to see if malloc returned a NULL result... if t and/or (n[i]+1) is sufficiently large, malloc() may be unable to get a chunk of memory big enough to satisfy the request, in which case you're trying to assign to a NULL pointer and you get a SIGSEGV -- there's a hint given in the description of the problem

Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)

Community
  • 1
  • 1
K Scott Piel
  • 4,320
  • 14
  • 19
  • SPOJ promises that `1 <= m <= n`, so `list[0] = list[1] = 0;` is okay here. – Daniel Fischer Apr 05 '13 at 19:27
  • Fair enough...and SPOJ gives a clue to why he's getting the SIGSEGV, as well... which circles around to my third point... [Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)] – K Scott Piel Apr 05 '13 at 19:35
0

Seems to work fine on my computer, except for the warning about using %ld (%lld should be used). I can only obtain a SIGSEGV error when putting 0 as a value for n[i]. Could you indicate what values you used to generate that error?

Edit : You are testing for the values "1 888888888 1000000000". Your computer simply can't allocate an array of such size. You are asking an array of size 1000000001 in your memory. That amounts to about 8GB (since a long long int as about 8B, at least on my computer), which is pretty much undoable for your computer.

Spam
  • 601
  • 4
  • 5
  • even on my pc it works fine for lower values ... but starts showing prblms for higher values . – Abhinav Gupta Apr 05 '13 at 19:18
  • 1
    @AbhinavGupta You're using too much memory. The above would need (roughly) 8GB. That's way more than SPOJ is willing to give you. You could try using one bit per flag to reduce memory usage. But You should definitely allocate a sieve only for the requested range. – Daniel Fischer Apr 05 '13 at 19:24
  • thnx a lot ... besides what u mentioned ... the datatype of m and n should also be long int tht's y it was not accepting higher ranges .. – Abhinav Gupta Apr 05 '13 at 20:17