Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bug "segmentation fault" in my c code

#include<string.h>
#include<stdio.h>


int firstState(char s[], int length);
int secondState(char s[], int length);
int thirdState(char s[], int length);
int forthState(char s[], int length);

int main()
{
    char string[10];

    gets(string);

    if( firstState(string, 0) )
        printf("Accept\n");
    else
        printf( "Not accept\n" );

    return 0;
}

int firstState(char s[], int length)
{  
    if(s[length] == 'a')
        return (secondState(s, length++));
    else if(s[length] == 'b')
        return firstState(s, length++);
    else
        return 0;
}

int secondState(char s[], int length)
{  
    if(s[length] == 'a')
        return secondState(s, length++);
    else if(s[length] == 'b')
        return thirdState(s, length++);
    else
        return 0;
}

int thirdState(char s[], int length)
{  
    if(s[length] == 'a')
        return secondState(s, length++);
    else if(s[length] == 'b')
        return forthState(s, length++);
    else
        return 0;
}

int forthState(char s[], int length)
{  
    if(s[length] == 'a')
        return secondState(s, length++);
    else if(s[length] == 'b')
        return firstState(s, length++);
    else
        return 0;
}

It gave me a segmentation fault or core dumped I'm confused!!! can someone explain why It gave me this kind of bug???? and tell how to debug to make my code run very clearly!!

I'm really tired with this :(

sorry for my bad English

like image 779
Bassam Badr Avatar asked Mar 06 '26 17:03

Bassam Badr


1 Answers

You have an infinite recursion,

return (secondState(s, length++));

The length argument passed is the value of length before the increment, so you only ever look at the first char.

Pass the length argument as length + 1, and check that length is smaller than 10 (the length of the char array string).

On another note,

gets(string);

is very unsafe, if the input is longer than nine characters, you write outside the allocated memory. Use

fgets(string, sizeof string, stdin);

instead.


Well, since it takes only the abovementioned fixes and the change of one return value, the most part of the logic was correct, the fixed code:

// #include<string.h> <- We don't use that
#include<stdio.h>

// Match the grammar (a+b)*abb

int firstState(char s[], int length);    // nothing of the suffix matched
int secondState(char s[], int length);   // matched one character of the suffix
int thirdState(char s[], int length);    // matched two
int forthState(char s[], int length);    // matched the complete suffix

int main()
{
    char string[10];
    // Get a 0-terminated string into the buffer.
    fgets(string, sizeof string, stdin);

    if( firstState(string, 0) )
        printf("Accept\n");
    else
        printf( "Not accept\n" );

    return 0;
}

int firstState(char s[], int length)
{  
    if(s[length] == 'a')  // first character of suffix matched
        return (secondState(s, length+1));
    else if(s[length] == 'b')  // nothing matched
        return firstState(s, length+1);
    else    // end of string in not-accepting state
        return 0;
}

int secondState(char s[], int length)
{  
    if(s[length] == 'a')  // the old matched 'a' wasn't part of the suffix, the new may be
        return secondState(s, length+1);
    else if(s[length] == 'b') // now matched two characters of the suffix
        return thirdState(s, length+1);
    else    // end of string in not-accepting state
        return 0;
}

int thirdState(char s[], int length)
{  
    if(s[length] == 'a')  // last three chars aba, the last 'a' could be part of the suffix
        return secondState(s, length+1);
    else if(s[length] == 'b')  // full suffix matched
        return forthState(s, length+1);
    else    // end of string in not-accepting state
        return 0;
}

int forthState(char s[], int length)
{  
    if(s[length] == 'a')  // another char, start a new candidate for the suffix
        return secondState(s, length+1);
    else if(s[length] == 'b')  // another char, can't be part of the suffix, start over
        return firstState(s, length+1);
    else        // end of string in accepting state, yay!
        return 1;
        // return s[length] == '\0';
        // if characters other than 'a' and 'b' need not signal the end of the string
}
like image 85
Daniel Fischer Avatar answered Mar 09 '26 07:03

Daniel Fischer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!