Why do I get a SIGSEGV?

Why I am getting a runtime error SIGSEGV in the problem? Where I am going wrong?

#include <stdio.h>
int main(void)
{
int i,j,n,a[100],t;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}

}
for(i=0;i<n;i++)
printf("%d\n",a[i ]);
return 0;

}

Why I am getting a runtime error SIGSEGV in the problem? Where I am going wrong?

#include <stdio.h>
int main(void)
{
int i,j,n,a[100],t;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if( a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}

}
for(i=0;i<n;i++)
printf("%d\n",a[i ]);
return 0;

}

because you are initializing array a of size 5 , whereas n can be very large , so you are trying to access invalid memory hence SIGSEGV

You have assumed that size of the array is 100 while it is given in the question that size can be up to 10^6. So in your for loop you will be accessing array out of bounds.

You are creating character array of length of 50 to store the given string whose length can be equal to 50. Each character array used to store string("%s" format specifier) automatically adds null character(’\0’) in the end. So you actually need length+1 memory to store.

@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