7 Replies - 439 Views - Last Post: 04 May 2013 - 07:24 PM Rate Topic: -----

#1 jaipurchanchal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-May 13

SIGSEGV run time error

Posted 04 May 2013 - 09:29 AM

this error keeps on appearing for every program i try to submit on spoj.com
problem statement: http://www.spoj.com/problems/CRAN02/
can anyone please tell me why this error shows up again nd again .. here's my code


#include<stdio.h>
int main(){
    int n,t,i,j,k,y,flag;
    scanf("%d",&t);
    while(t--){
               scanf("%d",&n);
               int count=0;
               int a[n];
               for(i=0;i<n;i++)
               scanf("%d",&a[i]);
               for(i=0;i<n;i++){
                                for(j=0;j<n;j++){
                                                 y=0;
                                                 flag=0;
                                                 for(k=i;k<=j;k++){
                                                 y+=a[k];
                                                 flag=1;}
                                                 if(y==0 && flag)
                                                 count++;
                                                 }
                                }
               printf("%d\n",count);
               }
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: SIGSEGV run time error

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: SIGSEGV run time error

Posted 04 May 2013 - 09:53 AM

Well unless you're compiling with a C compiler that supports the C99 standard the following is not allowed.

               scanf("%d",&n);

               int a[n];



To avoid problems I recommend you not use VLA (Variable Length Arrays) and you should probably stick with the C90 standard. This means your variables should be defined at the beginning of the scope, before any function calls or calculations.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 jaipurchanchal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-May 13

Re: SIGSEGV run time error

Posted 04 May 2013 - 10:01 AM

http://ideone.com/87FQrC
here i did declarations at the start but it is still showing an error
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: SIGSEGV run time error

Posted 04 May 2013 - 01:31 PM

Are you sure that none of the input values for n is greater than or equal to 100?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: SIGSEGV run time error

Posted 04 May 2013 - 01:38 PM

Quote

here i did declarations at the start but it is still showing an error

Please post your code in your post, not some external site.

Also post the exact complete error message.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 jaipurchanchal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-May 13

Re: SIGSEGV run time error

Posted 04 May 2013 - 01:42 PM

View PostSkydiver, on 04 May 2013 - 01:31 PM, said:

Are you sure that none of the input values for n is greater than or equal to 100?


i tried running it in ideone.It gave this error when i gave no input value to it

here it is and i will keep that in mind next time

#include<stdio.h>
int main(){
    int n,t,i,j,k,y,flag,count;
     int a[100];
    scanf("%d",&t);
    while(t--){
               scanf("%d",&n);
               count=0;
              
               for(i=0;i<n;i++)
               scanf("%d",&a[i]);
               for(i=0;i<n;i++){
                                for(j=0;j<n;j++){
                                                 y=0;
                                                 flag=0;
                                                 for(k=i;k<=j;k++){
                                                 y+=a[k];
                                                 flag=1;}
                                                 if(y==0 && flag==1)
                                                 count++;
                                                 }
                                }
               printf("%d\n",count);
               }
    return 0;
}



the error it shows is signal: 11 (SIGSEGV)
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: SIGSEGV run time error

Posted 04 May 2013 - 01:44 PM

View PostSkydiver, on 04 May 2013 - 04:31 PM, said:

Are you sure that none of the input values for n is greater than or equal to 100?


An looking more closely on your link to the puzzle, it looks like n is allow to be up to 1000000, but your array can only hold 100 items based on the code you posted on line #3.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: SIGSEGV run time error

Posted 04 May 2013 - 07:24 PM

View Postjaipurchanchal, on 04 May 2013 - 04:42 PM, said:

View PostSkydiver, on 04 May 2013 - 01:31 PM, said:

Are you sure that none of the input values for n is greater than or equal to 100?


i tried running it in ideone.It gave this error when i gave no input value to it
:
the error it shows is signal: 11 (SIGSEGV)


That is because your variables are uninitialized. If you give no input, the scanf() calls fail, and your code will continue to run with whatever random value you had in them prior to calling scanf(). So again, you can end up with a value of n that is beyond the bounds of your array.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1