Why do I get a SIGSEGV?

i am getting sigsegv in this code CodeChef: Practical coding for everyone
for this problem TSORT Problem - CodeChef
pls help

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int i,j,m, T,reverse=0;
// int arr[200];
int n[200];
T=atoi(argv[1]) ;
if(!((1<=T)&&(T<=1000)))
{
exit(0); //checking if T is not greater than 1000 and not less than 1
}
for(i=0,j=2;j<T+2;i++,j++)
{
n[i] = atoi(argv[j]) ;
if(!((1<=n[i])&&(n[i]<=100000) )) //checking if n is not greater than 100000 and not less than 1
{
exit(0); //(0 <= i) && ( i<=10)
}
}
/* printf("%d\n",T);

for(m=0;m<T;m++)
{
printf("%d\n",n[m]);
}
*/

for (m=0;m<T;m++)
 {
     reverse=0;
     while(n[m] != 0)
  {
  	 reverse = reverse * 10;
  	 reverse = reverse + (n[m] % 10 );
  	 n[m] = n[m] / 10;
  }
//printf("%d\n",n[m]);
	
printf("%d\n",reverse);

  }

return 0;
}

why m getting sigsegv

#include <stdio.h>
#include<stdlib.h>
int *intArray,n ;

void display(){
int i;
// navigate through all items
for(i = 0;i<n;i++){
printf("\n%d ",intArray[i]);
}
}

void swap(int num1, int num2){
int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}

int partition(int left, int right, int pivot){
int leftPointer = left -1;
int rightPointer = right;

while(1){

  while(intArray[++leftPointer] < pivot){
     //do nothing
  }
	
  while(rightPointer > 0 && intArray[--rightPointer] > pivot){
     //do nothing
  }

  if(leftPointer >= rightPointer){
     break;
  }else{
     swap(leftPointer,rightPointer);
  }

}
swap(leftPointer,right);
return leftPointer;
}

void quickSort(int left, int right){
if(right-left <= 0){
return;
}else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}

int main(){
int i;
scanf("%d",&n);
intArray=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
scanf("%d",intArray[i]);
quickSort(0,n-1);
display();
free(intArray);
return 0;
}
questions:–
All submissions for this problem are available.

Given the list of numbers, you are to sort them in non decreasing order.
Input

t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]

Output

Output given numbers in non decreasing order.
Example

Input:

5
5
3
6
7
1
Output:

1
3
5
6
7
why i am getting fragmentation error.?

why am i getting SIGEGV for this program CodeChef: Practical coding for everyone

whats wrong in this code. can some one help me out https://www.codechef.com/viewsolution/11554582

somebody please upvote me , i have questions to ask
thank you

This Error is related to Size of Array
The size of Array shall be ‘equal to or more than the no. of possible cases’.
eg: if there are 50 possible cases, maybe it’s not practical but your array size should be 50 or more.

At the same time, the Array size shall not be impractically large.
eg: A[1000000] will definitely give an Error.

Sometimes Dynamic Memory Allocation may also give this error.

plzz help y i m gtting SIGSEVG in this code CodeChef: Practical coding for everyone

Please help me. My code is giving a SIGSEGV and i am a noob. (LINK)

please help why am getting a sifsegv in this code

#include<stdio.h>
void main()
{
int n,j;
int flag=0,i=0;
int green[10],yellow[10],red[10],r_t=0,y_t=0,g_t=0;
scanf("%d",&n);
for(j=0;j<n;j++)
{
for(i=0;i<3;i++)
scanf("%d %d %d",&green[i+j],&yellow[i+j],&red[i+j]);

}
for(j=0;j<n*3;j++)
{
	
	
	g_t=g_t+green[j];
	y_t=y_t+yellow[j];
	r_t=r_t+red[j];
}
if(g_t/2!=0 && y_t/2==0 && r_t/2==0)
{
	printf("%d",g_t);
	flag=1;
}
else{
	if(g_t/2==0 && y_t/2!=0 && r_t/2==0)
	{
		printf("%d",y_t);
		flag=1;
	}
	else{
		if(g_t/2==0 && y_t/2==0 && r_t/2!=0)
		{
		printf("%d",r_t);
		flag=1;
	}
	}
	}
if(g_t/2!=0 && y_t/2!=0)
{flag=1;
	if(g_t>y_t)
	printf("%d",g_t);
	else
	printf("%d",y_t);
}
else{

	if(g_t/2!=0 && r_t/2!=0)	
	{flag=1;
	if(g_t>r_t)
	printf("%d",g_t);
	else
	printf("%d",r_t);
}
	else
		if(y_t/2!=0 && r_t/2!=0)	
	{flag=1;
	if(y_t>r_t)
	printf("%d",y_t);
	else
	printf("%d",r_t);
}
}
if(flag==0)
	{
		printf("0");
	}

}

can we create 2d array of 100000*100000
arr[100000][100000]

Why I am getting SIGSEGV.

https://www.codechef.com/viewsolution/12148465

Why am I getting a SIGSEVG for this code , CodeChef: Practical coding for everyone
Please help!

#include<stdio.h>
int main()
{
long long int t,n,i,j,m,max,k,r;
scanf("%lld",&t);
while(t–)
{
scanf("%lld",&n);
long long int a[n][n],p[n];
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
a[i][j]=0;
}
for(i=1;i<=n;i++)
scanf("%lld",&p[i]);
r=n-1;
for(i=1;i<=r;i++)
{
scanf("%lld%lld",&m,&n);
a[m][n]=1;
}
for(i=1;i<=n;i++)
{
max=0;
for(j=1;j<=n;j++)
{
if(i!=j)
{
if(a[i][j]==0 && a[j][i]==0)
{

							k=p[j];
							if(k>max)
							max=j;
						}
					}
			}
					printf("%  lld ",max);
					}	
				}

return 0;
}
where is the error

WHY DO I GET Runtime Error(SIGSEGV) FOR THIS SOLUTION OF PROBLEM:MIME2 Problem - CodeChef
#include <bits/stdc++.h>
using namespace std;
string s3,ans;
int main() {
int n,q;
cin>>n>>q;
string s1[n],s2[q];
for(int i=0;i<n;i++)
{
cin>>s1[i]>>s2[i];
}
while(q–)
{
ans=“”;
cin>>s3;
int x=s3.length();
int i=0;
while(s3[x-i-1]!=‘.’)
{
if(i==(x-1))
break;
i++;
}
if(s3[x-i-1]==‘.’)
{
while(i>0)
{
i–;
ans+=s3[x-i-1];
}
}
int flag=0;
for(int j=0;j<n;j++)
{
if(s1[j]==ans)
{
flag=1;
cout<<s2[j]<<endl;
break;
}
}
if(flag==0)
cout<<“unknown”<<endl;
}
}

@naman00b its because of condition for(i=1;i<=t;i++). Problem states that t is number of integers in number

Now, in an array, numbers are indexed from 0 to n-1. Meaning, the first number has index 0, second has index 2…and hence n has index n-1. In final iteration, it makes a reference to index n, for which array size should had been n+1. But since array is of size n, its’ maximum index is n-1. So when it calls for index n, it gives fault

(PS: Please upvote if you find the explanation correct and good :slight_smile: )

@shakku

If you can post the problem statement too, I can help. But seeing code, I suspect n is number of elements in array. Now -

for(i=1;i<=n;i++) { for(j=1;j<=n;j++) a[i][j]=0;

In every loop, you made index from 1 to n. But array has indexes from 0 to n-1. Try changing that 1 to n, to 0 to n-1.

(If my answer helps, please upvote ^^)

@bajajyukta

If you can give your submission link, I can have a better look over the code. :slight_smile:

Somebody please help me with this solution. It works on my machine.

But gives a SIGSEGV error here.

P.S. My compiler is in compliance with C99.