Runtime Error(SIGSEGV)

This type of problem generally comes due to array out of index problem.if such problem occurs see your loop consisting array index .happy coding. :slight_smile:

Hi
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.

source- codechef status code page

I am also receiving the same problem. My code works for the CLEANUP task. I have checked on IDEONE.com too and it also compiled it successfully. I couldn’t find any solution .Plz hellp!!
MY CODE:

#include<stdio.h>
using namespace std;
int main(){
    int t;
    scanf("%d", &t);
    if(t>50) return 0;
    for(int j=0;j<t;j++){
        int n,m,a,flag=0;
        scanf("%d%d", &n, &m);
        if(m<0 || m>n || n>1000) return 0;
        int arr[n]={0};
        for(int i=0;i<m;i++){
            scanf("%d",&a);
            arr[a-1]=-1;
        }
        for(int i=0;i<n;i++){
            if(arr[i]==-1) { flag++; }
            else { arr[i] = i-flag; }
        }
        for(int i=0;i<n;i++){
            if((arr[i]>-1) && (arr[i]%2==0)) {printf("%d ",i+1);}
        }
        printf("\n");
        for(int i=0;i<n;i++){
            if((arr[i]>-1) && (arr[i]%2==1)) {printf("%d ",i+1);}
        }
        printf("\n");
    }
    return 0;
}

It occurs when you access an element which is out of bound / else using too much memory

https://www.codechef.com/viewsolution/14108863
can someone pls help me with this one?I am a beginner and stuck in this for a while.I have checked all the possibilities for runtime error but cud nt find any.
Thanks
question : CodeChef: Practical coding for everyone

its against rules to discuss problems of live contest…! @akriti17
however runtime error is caused due to
1.using large memory(or declaring large sized arrays) …!! (for dis u have to declare large sized arrays globally like array[1000000]…)
2.accessing elements out of bounds i.e. accessing elements which are not declared !!
happy coding :slight_smile:

@tript,

 for(int i=0;i<m;i++){
            scanf("%d",&a);
            arr[a-1]=-1;
        }

This arr[a-1] can cause segmentation fault , check the bounds on a

You are accessing any index which is out of the bounds. Try to resolve the problem by carefully looking into the code . Happy Coding

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

int main() {
int n,k,sum=0;long a[100005];
// your code goes here
cin>>n>>k;
if(n>=1 && k<pow(2,100000)){
for(int i =0;i<k;i++)
{
a[i]=1;
sum+=a[i];
}
a[k]=k; int t;
for(int p=k+1;p<=n;p++)
{
t=0;
for(int j=1;j<=k;j++)
{
t=t+a[p-j];
}
a[p]=t;//%1000000007;
}
//cout<<a[n]<<endl;

/for(int l=0;l<n;l++)
{
cout<<"\t"<<a[l];
}
/
cout<<a[n-1]%1000000007;
}
return 0;

}

for the above code i’m getting SIGSEGV error when i try to submit. Can someone please help ?

does declaring an array of size 1000000 give a SIGSEGV because it occupies only 4MB (array type long long int)

I am also receiving the same problem. I have checked on IDEONE.com too and it also compiled it successfully. I couldn’t find any solution. It compiled sucessfully everywhere locally as well as on every online platform . But when I submit code it is showing same error as above .Plz hellp!! MY CODE:

#include<bits/stdc++.h>
using namespace std;
int main()

{
long long t;
cin>>t;
while(t–)
{
long long n,p,i,x=0,max=0,j,k,q,a;
vectorarr;
cin>>n>>p;
for(i=1;i<=p;i++)
{
a=n%i;
if(a!=0&&a<n)
arr.push_back(a);
}

   sort(arr.begin(),arr.end(),greater<int>());
    max=arr.front();
    for(i=1;i<=p;i++)
    {
        for(j=1;j<=p;j++)
        {
            for(k=1;k<=p;k++)
            {
                
                if(max==(((n%i)%j)%k)%n)
                x++;
            }
        }
    }
    cout<<x;
}

}

This type of error(segmentation fault) means that you are using an undefined memory and accessing it somewhere mostly in loops.

**Most of the time you need to declare an array of a[n+1] instead of a[n] bcz u loop i from 1 to n.

i am using arr size of n elements specified by the user and if i was accessing any element out of bounds i’d have received an error while it on other platforms too, but i didn’t.

Fixed the formatting.

int arr[n]={0};

I doubt this statement is allowed…not sure tho.

It is allowed, it initiallizes arr with 0 @vijju
@tript change the loop where you scanf to i=0;i<n;i++ it ll probably fix it, if it doesn’t declare a global array for being on the safe side.

Not sure. I distinctly remember using that statement once but it didn’t let me. And I had to use a loop to initialise it. I guess it must be that compiler in this case. Thanks for info @neilit1992 :slight_smile:

I guess all the obvious answers are here ,so i will tell you the different one that i faced.
dont declare variables of datatypes larger than as per required by the question. I declared some variables of type long long , which was causing me sigsegv , when i change them to int , the program compiled.

As it’s everywhere written the cause that using too much memory. I just want to know how much memory cause runtime errors??

#include <stdio.h>

int main(void) {
    int t;
    scanf("%d",&t);
    while(t>=1)
    {
        int n,count=0;
        scanf("%d",&n);
        int a[n];
        for(int i=0; i<n ; i++){
            scanf("%d", a[i]);
        }
        for(int i=0; i<n ; i++)
        {
            if(a[i]>=1000)
            {
                count++;
            }
        }
        printf("%d",count);
        t--;
    }
}

i am getting the Runtime Error(SIGSEGV), help please!!