Why do I get a SIGSEGV?

well… figured it out anyway…

line 22 (sscanf)
should have
&L,&R
instead of
L,R

This will only remove runtime error… it won’t give AC without any other changes :smiley:

why am i getting this runtime error for this code to execute patterns
output : 1 2 4
3 5 7
6 8 9

my code looks like :-

#include <iostream>
using namespace std;

int main () {
int i;
int N;
int num = 1;
cin>>N;
for (int z=0; z<N; z++) {
cin>>i;
int ARR[i][i];

for (int a=1,b=1; a<=i;a++){
 //This is to step down for i rows in a matrix represened by 2d array
    for (int x=a, y = b; y<=i; y++)
    {
        ARR[x][y]= -1;

    
    }
}  // have assigned every element of the array as -1


for (int a=1,b=1; a<=i;a++){
 //This is 11 21 31 
    for (int x=a, y = b; y<=i; y++)
    {
       for(int d=x , e=y; d<=i && e>=1; d++,e--) {
          if (ARR[d][e]==-1) {
              ARR[d][e] = num;
              num++;
          }
       }
    }
}

for (int a=1,b=1; a<=i;a++){
 //This is 11 21 31 
    for (int x=a, y = b; y<=i; y++)
    {
    
        cout<<ARR[x][y]<<" ";
        
    
    }
    cout<<endl;
} 
}
return 0;
}

the memory it sows to be used is 15.232 kB

can anyone also provide me with the amendments for the same code to run it successfully ??

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n,k,i,j;
cin>>n>>k;

    string a;
    vector<int> be;
    for(i=0;i<n;i++)
    {   
       
       cin>>a;
       vik.push_back(a);
     
       if(vik[i][n-1]=='1'&&i!=n-1)
       {   
           if(n-1-i<=k)
            be.push_back(i);
       }
    }
    if(be.size()==0)
    cout<<-1<<endl;
    cout<<22<<endl;
}

}

why sigsegv here??

because n is upto 10^6

Am getting sigsegv in this code
https://www.codechef.com/viewsolution/29406649
please help me

You’re not reading in the input correctly.

Try printing out the contents of your matrix by adding the lines:

                
        for(int i=0 ; i<n ; i++)
            for(int j=0 ; j<m ; j++)
                cout<<arr[i][j] << endl;

just before the line:

       sum = 0; count = 0;

and Run it with the sample testcase, and you should see your error :slight_smile:

2 Likes

Thank you man! This post really saved my life.

#include <stdio.h>
long int temp[10];
int main()
{
int t,m,j,r,a,b;
long int th,n,s;
scanf("%d",&t);
while(t–){
scanf("%ld %ld",&n,&th);
if(n>=2){
long int a[n],b[n],so;
for(j=0;j<n;j++){
scanf("%ld",&a[j]);
}
mergesort(a,0,n-1);

for(m=0;m<n;m++){
for(j=0;j<n;j++){
        r=a[j]-a[m];
so=(r>0)?r:0;
    s=s+so;
    }
    b[m]=s;
    s=0;
    if(b[m]<th){
        printf("%ld %ld",a[m-1],b[m-1]);
        break;}
}

printf("\n");

          }

}
return 0;
}
void mergesort(long int a[],int i,int j)
{
int mid;

if(i<j)
{
	mid=(i+j)/2;
	mergesort(a,i,mid);		//left recursion
	mergesort(a,mid+1,j);	//right recursion
	merge(a,i,mid,mid+1,j);	//merging of two sorted sub-arrays
}

}

void merge(long int a[],int i1,int j1,int i2,int j2)
{
//array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2)	//while elements in both lists
{
	if(a[i]<a[j])
		temp[k++]=a[i++];
	else
		temp[k++]=a[j++];
}

while(i<=j1)	//copy remaining elements of the first list
	temp[k++]=a[i++];

while(j<=j2)	//copy remaining elements of the second list
	temp[k++]=a[j++];


for(i=i1,j=0;i<=j2;i++,j++)
	a[i]=temp[j];

}

why sigsegv here?

Hello riapant2014,

Problem:-

Your code is giving SIGSEGV which is basically a segment fault error. This error occurs when you access the memory location out of your scope.
This problem also occurs when you are out of storage, Basically, CodeChef does not provide a huge amount of memory to run your program, So If you run your code in the local machine it will work fine. But in CodeChef IDE there is a constraint related to the stack memory. CodeChef provides memory around 64 MB which is even less than an array of size 100000. But in the question, it is given that it will provide n terms which are of the order 10^9. Hence your program will not get sufficient memory by CodeChef IDE and hence it will give SIGSEGV error.

Solution:-

We need not store all the n terms as to compute the modulo operation what we can do is Input the numbers and at the time of inputting we can directly check the modulo rather than storing it into an array.

Hope it Helps!!!
Happy Coding :slight_smile:

Thank you :slightly_smiling_face:

1 Like

why do i get SIGSEV for the problem CodeChef: Practical coding for everyone

I get “Access Denied” for that link.

I cleared the doubt. Thanks though

Really saved me. The thing to really check for is that you don’t store 1e7 memory in the main function itself! Thanks!

You have not declared vik vector.

You declared j but never used it , that’s why I think you’re getting runtime error

Thanks, great tips for solving the error I was running into

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--)
	{
	    int d;
	    cin >> d;
	    string s;
	    cin >> s;
	    int days;
	    int count_p=0;
	    for (int i=0;i<d;i++)
	    {
	        if (s[i]=='P')
	        {
	            count_p++;
	        }
	        
	    }
	    float req=0.75*d;
	    if (count_p>=req)
	    {
	        cout << '0' << endl;
	        break;
	    }
	    else
	    {
	        if (fmod(req,float(1))==0)
	        {
	            days=req-count_p;
	        }
	        else
	        {
	            days=1+(req-count_p);
	        }
	        
	    }
	    int count=days;
	    for (int j=2;(days!=0 || j<d-2);j++)
	    {
	        if (s[j]=='A')
	        {
	            if ((s[j-1]=='P' || s[j-2]=='P')&&(s[j+1]=='P' || s[j+2]=='P'))
	            {
	                s[j]='P';
	                days--;
	            }
	        }
	    }
	    if (days==0)
	    {
	        cout << count;
	    }
	    else
	    {
	        cout << "-1";
	    }
	    
	}
}

Why am i getting SIGSEGV Error??