Thread: SIGSEGV fault

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    SIGSEGV fault

    I'm getting runtime error (SIGSEGV) for the following code:
    Code:
    #include<stdio.h>
    #include <string.h>
       
    int lcs( char *A, char *B, int m, int n ){   
    int L[m+1][n+1];   
    int i, j;     
    for (i=0; i<=m; i++)   {     
    for (j=0; j<=n; j++)     {       
    if (i == 0 || j == 0)        
          L[i][j] = 0;       
     else if (A[i-1] == B[j-1])         
          L[i][j] = L[i-1][j-1] + 1;        
     else         
          L[i][j] = max(L[i-1][j], L[i][j-1]);     
        }   
    }    
    return L[m][n];
    }  
    
    int max(int p, int r){    
    return (p > r)? p : r;
    } 
    
    int main(){    
    int t;    
    scanf("%d", &t);    
    while(t--){  
    char X[25005];  char Y[25005];  
    scanf("%s%s", X,Y);   
    int len1 = strlen(X);  
    int len2 = strlen(Y);  
    int z = lcs(X,Y,len1,len2);    
    if(z == len1 || z == len2)printf("YES\n");    
    else printf("NO\n");    
    }  
    return 0; 
    }
    What can be the reason?
    Last edited by Salem; 05-03-2013 at 01:28 PM. Reason: Fixed the fugly formatting - next time, paste as text, not HTML

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by i_wanna_rokk View Post
    Code:
    int L[m+1][n+1];
    Code:
    char X[25005]; char Y[25005];
    Probably the size of the arrays is too large to allocate them on the stack, use dynamic allocation instead.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Code:
    $ gcc -g -Wall foo.c
    foo.c: In function ‘lcs’:
    foo.c:14:7: warning: implicit declaration of function ‘max’ [-Wimplicit-function-declaration]
    $ ./a.out 
    1
    hello
    world
    NO
    $ ./a.out 
    1
    hello
    hello
    YES
    You need to provide a crashing test case if you want more help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 24
    Last Post: 02-09-2011, 09:19 AM
  2. blocking SIGSEGV
    By sleith in forum C++ Programming
    Replies: 14
    Last Post: 10-21-2008, 07:31 PM
  3. SIGSEGV, Segmentation fault
    By micmac700 in forum C Programming
    Replies: 3
    Last Post: 12-13-2006, 03:47 PM
  4. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM
  5. SIGSEGV, segmentation fault
    By StuBarnes4Prez in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2002, 09:26 PM