Thread: Sig fault

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    27

    Sig fault

    Code:
    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    const char* search1="";
    const char* search2="apple";
    const char* search3="apple tree";
    
    char* strings1[3]={"","",""};
    char* strings2[3]={"app","apple treehouse","apple treehouse"};
    
    char* match(const char* search, char** strings, int n) {
       if (n==0) {
          return "";
       }
       if (n==1) {
          if (!strcmp(search, "")) {
             return strings[0];
          }
          else {
             if (!strncmp(strings[0], search, strlen(search))) {
                return strings[0];
             }
             else return "";
          }
       }
       
      
       if (n>1) {
          int i=1;
          int j=n-1;
          int k=1;
          char result[20]="";
          if (!strcmp(search, "")) {
             while (j--) {
                if (!strncmp(strings[0], strings[i++], k)) continue;
                else return "";
             }
             strncpy(result, strings[0], 1);
             result[1]='\0';
             char* r=(char*)malloc(20*sizeof(char));
             strcpy(r, result);
             k++;
             i=1;
             j=n-1;
             while (k++<=strlen(strings[0])) {
                while (j--) {
                   if (!strncmp(strings[0], strings[i++], k)) continue;
                   else return r;
               }
               strncpy(result, strings[0], k);
               result[k]='\0';
               strcpy(r, result);
               puts(r);
             }
             strcpy(r, strings[0]);
             return r;
          }
       }
    }
    
    int main() {
       match(search1, strings2, 3);
       return 0;
    }
    This gives seg fault. Why? Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    For starters, line 48 (the strncmp) inevitably will access the array out of bounds..

    Also, if you are making anything larger out of this, you need to get rid of the globals.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    Quote Originally Posted by memcpy View Post
    For starters, line 48 (the strncmp) inevitably will access the array out of bounds..

    Also, if you are making anything larger out of this, you need to get rid of the globals.
    Thanks. I had to reset i and j to their original values inside while loop. Here's code returning "app" as expected after matching.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    const char* search1="";
    const char* search2="apple";
    const char* search3="apple tree";
    
    char* strings1[3]={"","",""};
    char* strings2[3]={"app","apple treehouse","apple treehouse"};
    
    char* match(const char* search, char** strings, int n) {
       if (n==0) {
          return "";
       }
       if (n==1) {
          if (!strcmp(search, "")) {
             return strings[0];
          }
          else {
             if (!strncmp(strings[0], search, strlen(search))) {
                return strings[0];
             }
             else return "";
          }
       }
       
      
       if (n>1) {
          int i=1;
          int j=n-1;
          int k=1;
          char result[20]="";
          if (!strcmp(search, "")) {
             while (j--) {
                if (!strncmp(strings[0], strings[i++], k)) continue;
                else return "";
             }
             strncpy(result, strings[0], 1);
             result[1]='\0';
             char* r=(char*)malloc(20*sizeof(char));
             strcpy(r, result);
             k++;
             i=1;
             j=n-1;
             while (k++<=strlen(strings[0])) {
                while (j--) {
                   if (!strncmp(strings[0], strings[i++], k)) continue;
                   else return r;
               }
               i=1; j=n-1;
               strncpy(result, strings[0], k);
               strcpy(r, result);
             }
             return r;
          }
       }
    }
    
    int main() {
       puts(match(search1, strings2, 3));
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 04-27-2006, 02:29 AM
  2. i have a seg fault
    By ck1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 12:02 AM
  3. seg fault
    By chrismiceli in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 07:33 PM
  4. Seg fault out of no where
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 02-25-2003, 08:51 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM