Why do I get a SIGSEGV?

Why I am getting SIGSEGV error on TSORT problem. I am using C and here is my Solution

why do i get SIGSEGV in this code please help me…
https://www.codechef.com/viewsolution/10061505

please help why do i get sigsegv in this code CodeChef: Practical coding for everyone

#include<stdio.h>
int main()
{
int r,i,j,k,l,p,q,x;
int n,m,T;
int B[n],A[n-m];
scanf("%d",&T);
for(k=0;k<T;k++)
{
scanf("%d%d",&n,&m);
for(r=0;r<n;r++)
{B[r]=0;
}

 	 for(l=0;l<m;l++)
 	 {
 	 	 scanf("%d",&x);
 	 	 B[x-1]=1;
	  }
	  
for(i=0,j=0; i<n,j<n-m; i++)
{
	 if(B[i]==0)
	 A[j++]=i+1;
	 
 }
 
 for(q=0;q<n-m;q=q+2)
 {printf("%d ",A[q]);
  A[q]=0;
	   }	
printf("\n");	   
		     
for(p=0;p<n-m;p++)
	  {
	  	if(A[p]!=0)
	  	printf("%d ",A[p]);
	  }
	printf("\n");	  
	  
	  
	   }
	   return 0;

}

why am i getting this error here???pls help

Why are my programs getting runtime error (SIGSEV) during submissions but run well in all IDEs I’ve tried?
I’m pretty sure it’s not a case of out-of-range array access. What are the other possible faults?

1 Like

please check this this code works fine on codechef ide ,ideone.com but every time when i am submitting i am getting runtime error SIGSEGV CodeChef: Practical coding for everyone

  1. I do not quite understand why there are so many people have posted their own questions as answers for this question. Probably, this isn’t well moderated or there is something that I do not understand.
  2. Since, there is no code provided in the question the reason for getting SIGSEGV could not be determined. I am surprised that this old thread has 61 answers when actually there is no relevant information.

Refer Code Chef Wiki about status codes

why m getting SIGSEGV RUNTIME ERROR for problem LCOLLIS Problem - CodeChef solution is CodeChef: Practical coding for everyone

SIGSEV error pops up due to a segmentation fault

Possible reasons for segmentation fault are :

  • One of the array indices you used would’ve been an invalid index . This means that at some point of execution , your program uses a value for the array index that is either more than the size of the array or less than 0
  • When you try to dereference a pointer with NULL value
  • When you declare an array of a very huge size . In this case the compiler itself can’t handle the array . Hence it pops out segmentation fault

Try adhering to all of the specifications as mentioned by the admin

Also , this link may help you

getting SIGSEV onthis piece of code which is from the beginner fctrl problem

#include
using namespace std;
int main()
{
int num,n=1,zeroes=0,power=5;
cin>>num;
int i,j,k,arr[1000],res[1000];
for(i=0;i<num;i++)
{
cin>>arr[i];
}
for(j=0;j<num;j++)
{
zeroes=0;
power=5;
while((arr[j]/power)>0)
{
zeroes+=(arr[j]/power);
n++;
power*=5;
}
res[j]=zeroes;
}
for(k=0;k<num;k++)
cout<<res[k]<<endl;
return 0;
}

please help tried to write a very precise piece of code but it seems not to be working help;

why do i get SIGSEVAG for this code ?
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int m,n,i,indices,count=0;
cin>>n>>m;
int a[n]={0};
while(m–){
cin>>indices;
a[indices-1]=1;
}

	  for(i=0;i<n;i++){
	  	if(a[i]!=1){
		  
	  	count++;
	  	if(count%2==0&&count!=0)
	  	a[i]=3;
	  	if(count%2==1)
	  	a[i]=4;		     
	  }
}
	  
	for(i=0;i<n;i++){
		if(a[i]==4)
		cout<<i+1<<" ";

}
cout<<endl;
for(i=0;i<n;i++){

		if(a[i]==3)
		cout<<i+1<<" ";
	}
	cout<<endl;
}
return 0;

}

@vineeta1995
.Condition if(t[I]℅k==0)
Will give run time error if value of k=0;

@ankittt_jain you have declared array a[n]
If the value of n goes above 10^6 then you will get run time error.

please help why do i get SIGSEVG for this code
https://www.codechef.com/viewsolution/10818136

You can check here for all errors that mostly occurs at codechef https://www.codechef.com/wiki/status-codes

SIGSEGV : A SIGSEGV is an error(signal) caused by an invalid memory reference or a segmentation fault. You are probably trying to access an array element out of bounds or trying to use too much memory. Some of the other causes of a segmentation fault are : Using uninitialized pointers, dereference of NULL pointers, accessing memory that the program doesn’t own

@gokul95
“solution is CodeChef: Practical coding for everyone

You are getting SIGSEGV becuase you have take character pointer char* a and inputting directly into “a” by using scanf(“%s”,a); This is wrong. “a” is a pointer and has no memory alocated to it. You should first allocate memory to the pointer using malloc before taking inout.
a=(char* )malloc(sizeof(char)*SIZE)
where SIZE is the maximum input lenght of string + 1(1 extra for ‘\0’).
Just add this line before your scanf line.

I have tried many things and this code runs fine on my computer but on submitting I repeatedly get SIGSSEVG ?? You can check here.CodeChef: Practical coding for everyone

why do i get sigsevg for this code
https://www.codechef.com/viewsolution/11028794

why do i get sigsevg for the code : CodeChef: Practical coding for everyone