Thread: Help! segmentation fault

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Question Help! segmentation fault

    Hi Iam getting a segmentation fault in the following program, it would be great if you can suggest where exactly iam going wrong, here Iam trying to extract substrings from a string and store it in a array of strings, please note the recursive call in the function.

    Code:
    
    
    }
    Last edited by doubty; 06-23-2009 at 03:39 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    parser(label,label);
    What are you doing this for?
    Code:
    void parser(char in_str[][25],char out_str[][25])
    Shouldn't you be paying attention to the number of rows you have here, instead of just assuming there's an unlimited supply? Typically when you leave out the size of one of the dimensions, you actually pass something along to let the function know when to stop.

    You really need to learn to indent if you want people to read your code. No one wants to read that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Exclamation

    Quote Originally Posted by doubty View Post
    Hi Iam getting a segmentation fault in the following program, it would be great if you can suggest where exactly iam going wrong, here Iam trying to extract substrings from a string and store it in a array of strings, please note the recursive call in the function.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    #include<stdlib.h>
    
    int k=0;
    int p=1;
    void parser(char in_str[][25],char out_str[][25]);
    
    //Main function
    int main()
    {
    
        char inputexp[500];
        char label[50][25];
    
    
       printf("enter expression");
        scanf("%s",inputexp);
    
       strcpy(label[0],inputexp);
      
      //calling function parser
    
      parser(label,label);
    
       return 0;
    
      getchar();
    
    }
    
    //function parser
    
    void parser(char in_str[][25],char out_str[][25])
    {
         while(in_str[k]!= '\0')
               {
                    int len=strlen(in_str[k]);
                    int openbrac_count=0,closebrac_count=0;
                    int i=0;
                     int from=0;
                     while(i<len)
                           {
                                while(i<len && in_str[k][i]!=')')
                                      {
                                              if((in_str[k][i]=='('))
                                                 {
                                                     openbrac_count++;
                                                    }
                                        i++;
                             }
                     while(i<len && in_str[k][i]!='(')
                            {
                                 if(in_str[k][i]==')')
                                     {
                                           closebrac_count++;
                                       }
    
                                if(openbrac_count==closebrac_count & openbrac_count != 0)
                                        {
                                             strncpy(out_str[k+1],&in_str[k][from],i-from);
                                               p=p+1;
    
                                             if (in_str[k][i+1]=='&' || in_str[k][i+1]=='|')
                                                      {
    
                                                        from=i+2;
    
                                                         }
    
                                        }
    
                                       i++;
    
                               }
                 if (in_str[k][0]=='G')
                {
                strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                 parser(in_str,out_str);
                  }
    
                  if (in_str[k][0]=='F')
                 {
                   strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                    parser(in_str,out_str);
                   }
    
                if (in_str[k][0]=='X')
               {
                 strncpy(out_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                  parser(in_str,out_str);
                }
             }
    
             k++;
    
            } //end of function parser
    
    }
    Hi ,

    Srry abt the indentation, when i pasted it frm the file i lost the indentation, anyways this is what i have been trying to do,

    I have a String say G(G(G(xxxx))) > yyyyy
    I have to extract all the substings of the string namely {G(G(G(xxxx)))>yyyyy,G(G(G(xxxx))),G(G(xxxx)),G(xx xx),xxxx,yyyyy}

    I have in my main program accepted the string and put it as the first element of the array of strings label,

    Now I have written the function parser that takes 2 arrays,{the input and the output array} in fact bothe are the same, and calculates all the substrings and stores in it. So upfront i dunno how many rows are present in the array.

    I have used recursion coz of the pattern in the string.

    I know iam going wrong somewheer, can you please guide me

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    I hope the above details are sufficient to help me

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The logic seems awkward. I suggest using a simple and separate char buffer for your string, and using just *one* array for your output.

    label and label both obviously point to the base of the same array, so now you have to dance around the big elephant in the living room, which is that both arrays are actually the same array.

    Clear and simple is what you need, not this "I'll use one array, but dance around with it like it's really two array's". Easy to debug, easy to maintain, and modify, and easy to have others understand, etc.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Exclamation

    Hi Adak,

    I changed it as below:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    #include<stdlib.h>
    
    int k=0;
    int p=1;
    void parser(char in_str[][25]);
    
    //Main function
    int main()
    {
    
        char inputexp[500];
        char label[50][25];
    
    
       printf("enter expression");
        scanf("%s",inputexp);
    
       strcpy(label[0],inputexp);
      
      //calling function parser
    
      parser(label);
    
       return 0;
    
      getchar();
    
    }
    
    //function parser
    
    void parser(char in_str[][25])
    {
         while(in_str[k]!= '\0')
               {
                    int len=strlen(in_str[k]);
                    int openbrac_count=0,closebrac_count=0;
                    int i=0;
                     int from=0;
                     while(i<len)
                           {
                                while(i<len && in_str[k][i]!=')')
                                      {
                                              if((in_str[k][i]=='('))
                                                 {
                                                     openbrac_count++;
                                                    }
                                        i++;
                             }
                     while(i<len && in_str[k][i]!='(')
                            {
                                 if(in_str[k][i]==')')
                                     {
                                           closebrac_count++;
                                       }
    
                                if(openbrac_count==closebrac_count & openbrac_count != 0)
                                        {
                                             strncpy(in_str[k+1],&in_str[k][from],i-from);
                                               p=p+1;
    
                                             if (in_str[k][i+1]=='&' || in_str[k][i+1]=='|')
                                                      {
    
                                                        from=i+2;
    
                                                         }
    
                                        }
    
                                       i++;
    
                               }
                 if (in_str[k][0]=='G')
                {
                strncpy(in_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                 parser(in_str);
                  }
    
                  if (in_str[k][0]=='F')
                 {
                   strncpy(in_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                    parser(in_str);
                   }
    
                if (in_str[k][0]=='X')
               {
                 strncpy(in_str[p+1],&in_str[k][2],strlen(in_str[k])-2);
                  parser(in_str);
                }
             }
    
             k++;
    
            } //end of function parser
    
    }
    Iam getting segmentation fault while calling the function, can you please help me now

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so say you're extracting strings. What is it you expect your end string to look like? Given:

    a( b( c( d ) ) )

    What would your end string look like? Is it just one string all squished together somehow? Is it a series of strings?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    Ok, so say you're extracting strings. What is it you expect your end string to look like? Given:

    a( b( c( d ) ) )

    What would your end string look like? Is it just one string all squished together somehow? Is it a series of strings?


    Quzah.
    This is what also bothers me. I believe it should parse out as separate strings to be:

    What's left after every recursion/iteration:
    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d

    What's parsed out for every recursion/iteration:
    a( )
    a( b( ) )
    a( b( c( ) ) )
    a( b( c( d ) ) )

    Which I don't see in his example:

    I have a String say G(G(G(xxxx))) > yyyyy
    I have to extract all the substings of the string namely {G(G(G(xxxx)))>yyyyy,G(G(G(xxxx))),G(G(xxxx)),G(xx xx),xxxx,yyyyy}
    Doubty, can you take Quzah's example and show it being correctly parsed?

    To me, "all substrings", would mean *all* substrings:
    Code:
    {
    G
    (
    G
    (
    ...
    {G
    G(
    etc.

    Clearly, that's not what you want.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Exclamation

    Quote Originally Posted by Adak View Post
    This is what also bothers me. I believe it should parse out as separate strings to be:

    What's left after every recursion/iteration:
    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d

    What's parsed out for every recursion/iteration:
    a( )
    a( b( ) )
    a( b( c( ) ) )
    a( b( c( d ) ) )

    Which I don't see in his example:



    Doubty, can you take Quzah's example and show it being correctly parsed?

    To me, "all substrings", would mean *all* substrings:
    Code:
    {
    G
    (
    G
    (
    ...
    {G
    G(
    etc.

    Clearly, that's not what you want.
    Hi Adak and quzah,

    For the example a( b( c( d ) ) ) my substring would be

    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d

    However said that the strings may not be as simple in the sense that it may also be as

    a( b( c( d ) ) ) ^ e(f(g)))

    in which case my substring set would be:

    a( b( c( d ) ) ) ^ e(f(g)))
    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d
    e(f(g)))
    f(g)
    g


    Another constraint is that i can also have a(b(a(c)))
    i.e 'a' repeting more than once ( it is for this i have tried to use recursion)

    for the above case my substrings would be:
    a(b(a(c)))
    b(a(c))
    a(c)
    c


    I hope I am clearer now, Thanks a lot for your efforts to help me out

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The first and last examples you gave, were fine, but this one is trouble, imo:
    However said that the strings may not be as simple in the sense that it may also be as

    a( b( c( d ) ) ) ^ e(f(g)))

    in which case my substring set would be:

    a( b( c( d ) ) ) ^ e(f(g)))
    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d
    e(f(g)))
    f(g)
    g
    Because you're parsing out the ^e(f(g))), and then you're bringing it back to be re-parsed.

    IMO you need a block of code (or a function), to "feed" the recursive parsing function. The "feeder" will record the whole string as an answer, then remove any part of the string including and after the ^ , and send it to the parsing function only after the leftmost part before the ^ has been parsed.

    That "feeding" function seems like a good candidate for recursion, itself.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's not recursive, but this is my take on parsing this expression

    Code:
    /* Parsing.c
    
    a( b( c( d ) ) ) ^ e(f(g))
    
    in which case my substring set would be:
    
    a( b( c( d ) ) ) ^ e(f(g))
    a( b( c( d ) ) )
    b( c( d ) )
    c( d )
    d
    e(f(g))
    f(g)
    g
    
    */
    
    #include <stdio.h>
    #include <string.h>
    
    int main(void)  {
       int i, lt, rt, hasFlex = 0;
       char *ptr = NULL;
       char s[] = {"a( b( c( d ) ) ) ^ e(f(g))"};
       printf("\n\n\nOriginal String: \n%s", s);
    
       ptr = strchr(s, '^');  //does the string have a circumflex
       if(ptr) {
          rt = ptr - s;  
          hasFlex = rt;
       }
       else
         rt = strlen(s);
       lt = 0;
       //printf("\n lt: %d, rt: %d", lt, rt);
       do  {
          if(s[lt++] == '(') {
             while(s[rt--] != ')' && lt < rt);
             putchar('\n');
             for(i = lt; i <= rt; ++i)
                printf("%c", s[i]);
             //i = getchar();   
          }
          if(lt >= rt && hasFlex) {  //a circumflex component to deal with
             lt = hasFlex+1;
             rt = strlen(s);
             hasFlex = 0;
             putchar('\n');
             for(i = lt; i <= rt; ++i)
                printf("%c", s[i]);
          }
       }while(lt <= rt);
    
       printf("\n\n\t\t\t     Press Enter When Ready ");
       lt = getchar(); 
       return 0;
    }
    The string sample you posted had 1 extra right parentheses, which I removed.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi thanks a lot for your help, but this is too specific for the case where in I have only one ^ symbol between 2 set od substrings , in my case i can have any number of ^ symbols and substrings in between (i.e A(A(d)) ^ B(B(g)) v C(C(h)) ^ D(D(h)), Also please note that i can also have 'V' symbol as well.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh, you're just *full* of good news, aren't you!!

    So change the code to handle a ^ or a V, in the same way a ^ is handled now.

    And add either logic inside the do while loop to handle multiple ^'s or V's, or you can add another do while loop outside the current do while loop, to basically, keep it looping while the string has more separating ^'s or V's with substrings of their own.

    Basically, you just want to move, from left (lt) to right(rt) across the string, and see if there are any ^'s or V's.

    If there are, put the rt index at the leftmost one of them.

    Now parse the substring from lt (0), to the rt index, in the string s.

    Afterward, set lt at rt, and scan the string for any more separators (^ or V). If you find one, set the rt index there, and parse the substring between lt and rt.

    Repeat until there are no more separators, then process your last substring.

    When your string has no more separators, and no more parenthesis, you're done.
    Last edited by Adak; 06-24-2009 at 12:27 AM.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi ,

    I know you will kill me for this ..

    I was also checking out if i had strings in any other particular format and i hit upon this one:

    i have a very very peculiar requirement :

    i.e my input can also be as G(G(p^q)) ^ G(q)

    the problem here is that if I go by finding the first occurence of ^ and demarkate it doesnt work.
    i.e

    my strings need to be :
    G(G(p^q)) ^ G(q)
    G(G(p^q))
    G(p^q)
    p^q
    p
    q
    G(q)

    i.e ther's a difference between the ^ inside the bracket and the one outside,
    Thanks

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by doubty View Post
    i.e ther's a difference between the ^ inside the bracket and the one outside,
    Thanks
    How does what you posted indicate a difference between a ^ inside a bracket and one outside? In each case, each subexpression of ^ needs to be in your list (as well as the ^ expression itself).

    I don't know the context of why you're doing this, exactly, but given that these are logical expressions is there a reason you don't treat them as such? (I.e., make a tree out of this using the shunting yard algorithm and then traverse the tree to get all your subexpressions?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM