SIGSEGV error

#include
#include<string.h>
using namespace std;

int sol[5000][10];
char lex[5000],lext[5000];
int n[5],cases;


void inputd()
{
 cin>> cases ;
 if(cases<6)
  for(int t=0;t<cases;t++)
  {
   cin >> n[t];
   if(n[t]<5001)
   for(int i=0;i<n[t];i++)
   {
    cin >> sol[i][t*2] >> sol[i][t*2+1];
   }
  } 
}

int isI(int i,int t)
{
 int l=sol[0][0];
 int j=0;
 int k=i;
 if(i)
 {
  for(;j<n[t]&&k>-1;j++)
  {
   if(sol[j][t*2]<=i&&sol[j][t*2+1]>=i)
   {
 
    k--;
    lext[j]='1';
   }
   else
    lext[j]='0';
  }
  
  if(k==0)
  {
   if(strcmp(lex,lext)>0)
    for(j=0;j<n[t];j++)
     lex[j]=lext[j];
   return true;
  }
  return false;
 }
 else
 {
  
  for(j=0;j<n[t];j++)
  {
   if(sol[j][t*2]==0)
   { 
    return 0;//0 not possible
   }
  }
  for(j=0;j<n[t];j++)
  {
   lex[j]='0';
  }
  return 1;
 }
// return true;
}

void clearlex(int t)
{
 int i=0;
 for(;i<t;i++)
 {
  lex[i]=lext[i]='1';
 }
 lex[i]=lext[i]='\0';
}

int main ()
{
 int a,c=0,t=0;
 inputd();
// sol={1,4,2,,4,3,4,4,4}};
 for(;t<cases;t++)
 {
  clearlex(n[t]);
  c=0;
  for(int i=0;i<=n[t];i++)
  {
   if(isI(i,t))
   c=(c+1)%1000000007;
  }
  cout <<endl << lex << endl << c << endl;
 }
 cin >> a;
 return 0;
}

This is my code and i am getting a runtime error SIGSEGV. I understand that this is error occcurs due memory segmentation faults. But, i don’t unerstand what to change in my code to avoid that.

Thanks in advance

Hello,

I do not recognise the problem you are trying to solve. I do notice that n[i] can be at most 5000. In clearlex, parameter t is therefore also at most 5000. After the loop, i can therefore be 5000. The statement lex[i]=lext[i]=’\0’; will then overrun the array boundary…

Two other observations:

  1. It seems that you read in the test cases and then calculate the results. Your program may become simpler (and use less memory) if you read each test case and immediately calculate its result.
  2. The statement c=(c+1)%1000000007 raises questions. This piece of code can be run at most 5000 times after c has been set to 0. The % operator will not have any effect. I guess there is some problem in the logic here…

Don’t forget to remove cin >> a before you submit the code…

1 Like

Have you gone through the FAQs? Explanation for SIGSEGV is given there.

Quoting from the wiki-

This is an error caused by an invalid
memory reference or segmentation
fault. The most common causes are
accessing an array element out of
bounds, or using too much memory.

Some things for you to try:

  • Make sure you aren’t using variables that haven’t been
    initialised. These may be set to 0 on
    your computer, but aren’t guaranteed
    to be on the judge.
  • Check every single occurrence of accessing an array element and see if
    it could possibly be out of founds.
  • Make sure you aren’t declaring too much memory. 64 MB is guaranteed, but
    having an array of size [10000][10000]
    will never work.
  • Make sure you aren’t declaring too much stack memory. Any large arrays
    should be declared globally, outside
    of any functions - putting an array of
    100000 ints inside a function probably
    won’t work.

For me, most of the time the reason is - accessing array offset beyond the size of array.

EDIT - To track the SIGSEGV, you should run your program in gdb, or the debugger present in your IDE. The debugger stops at exact point of SIGSEGV, so you can examine the line of code just before that. You can also look at the call stack.

1 Like

second observation was obviously and unnecessarily stupid statement…
the error has been shunned…all the suggestions you mentioned were useful…thanks