Why do I get a SIGSEGV?

@riapant2014:

How do u declare array of unknown size?.
so after taking the input, u have to declare .i.e int q[n].

below is my accepted solution with just change of one line in your code

http://www.codechef.com/viewsolution/6348707

1 Like

Cant access!!

Is it in python?

no, it is c…this is the problem

and this the code
#include <stdio.h>
long long int a[1000006];
int main()
{
long long int n,k,i,co=0,max=0;
scanf(“%lld %lld”,&n,&k);
for(i=1;i<=n;i++)
{
scanf(“%lld”,&a[i]);
if(max<a[i])
max=a[i];
}
long long int c[max+1];
for(i=0;i<=max;i++)
c[i]=0;
for(i=1;i<=n;i++)
c[a[i]]++;
for(i=1;i<k;i=i+2)
{
if(c[i]>0)
co++;
}
printf(“%lld\n”,co);
return 0;
}
thank u…

aUtama[10] aCari[10]
You are declaring maximum size as 10. However constraint on maximum size is sadly not specified. Set it to 10000 as that worked in many accepted solutions.

life saver!

U can declare a array of size [10000][10000] globally . and it will not give sigsegv error

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