cstack-smash

***stack smashing detected***


When i run my code on windows (CodeBlocks) everything works but when i run in on Linux it says stack smashing detected and doesnt print anything but the board; Here ius the code:

#include <stdio.h>
#include <stdlib.h>



void inimatri(char mat[7][7]){

    int l=0,c=0;


    while (l<8){
        while (c<8){
            mat[l][c]='.';
            c=c+1;
        }
    c=0;
    l=l+1;
    }

    mat[3][3]='O';
    mat[3][4]='X';
    mat[4][3]='X';
    mat[4][4]='O';
    mat[2][5]='O';
    mat[5][2]='O';
    mat[3][2]='O';
    mat[5][4]='O';


}
void showmat (char mat[7][7]){

    int l=0,c=0;


    printf("  0 1 2 3 4 5 6 7");

    while (l<8){
        printf( "\n%d",l);
        while (c<8){
            printf( " %c",mat[l][c]);
            c=c+1;
        }
    c=0;
    l=l+1;
    }


}

int hord(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (c== 7){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l][c+1]==pecaac){
        return 0;
    }
    while ((c+1)<8){
        if (mat[l][c+1]=='.'){
            return 0;

        }
        else if (mat[l][c+1]== pecaac){
            return 1;
        }
        else{
            legal= 0;
        }
     c=c+1;
    }
    return(legal);
}

int hore(char mat[7][7], int l, int c, char pecaac){
    int legal=0;

    if (c== 0){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l][c-1]==pecaac){
        return 0;
    }
    while (0<=(c-1)){
        if (mat[l][c-1]=='.'){
            return 0;

        }
        else if (mat[l][c-1]== pecaac){
            return 1;
        }
        else {
            legal=0;
        }

     c=c-1;
    }
    return (legal);
}

int verb(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 7){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l+1][c]==pecaac){
        return 0;
    }
    while ((l+1)<8){
        if (mat[l+1][c]=='.'){
            return 0;

        }
        else if (mat[l+1][c]== pecaac){
            return 1;
        }
        else{
            legal=0;
        }
     l=l+1;
    }
    return (legal);
}

int verc(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 0){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l-1][c]==pecaac){
        return 0;
    }
    while (0<=(l-1)){
        if (mat[l-1][c]=='.'){
            return 0;

        }
        else if (mat[l-1][c]== pecaac){
            return 1;
        }
        else{
            legal = 0;
        }
     l=l-1;
    }
    return (legal);
}

int diads(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 0 || c== 7){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l-1][c+1]==pecaac){
        return 0;
    }
    while (0<=(l-1) && c+1<8){
        if (mat[l-1][c+1]=='.'){
            return 0;

        }
        else if (mat[l-1][c+1]== pecaac){
            return 1;
        }
        else{
            legal = 0;
        }
     l=l-1;
     c=c+1;
    }
    return (legal);
}

int diadi(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 7 || c== 7){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l+1][c+1]==pecaac){
        return 0;
    }
    while ((l+1)<8 && (c+1)<8){
        if (mat[l+1][c+1]=='.'){
            return 0;

        }
        else if (mat[l+1][c+1]== pecaac){
            return 1;
        }
        else{
            legal = 0;
        }
     l=l+1;
     c=c+1;
    }
    return (legal);
}
int diaes(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 0 || c== 0){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l-1][c-1]==pecaac){
        return 0;
    }
    while (0<=(l-1) && 0<=(c-1)){
        if (mat[l-1][c-1]=='.'){
            return 0;

        }
        else if (mat[l-1][c-1]== pecaac){
            return 1;
        }
        else{
            legal = 0;
        }
     l=l-1;
     c=c-1;
    }
    return (legal);
}

int diaei(char mat[7][7], int l, int c, char pecaac){
    int legal;
    if (l== 7 || c== 0){
        return 0;
    }
    if (mat[l][c]!='.'){
        return 0;
    }
    if (mat[l+1][c-1]==pecaac){
        return 0;
    }
    while ((l+1)<8 && 0<=(c-1)){
        if (mat[l+1][c-1]=='.'){
            return 0;

        }
        else if (mat[l+1][c-1]== pecaac){
            return 1;
        }
        else{
            legal = 0;
        }
     l=l+1;
     c=c-1;
    }
    return (legal);
}

int clegal(char mat[7][7], int l, int c, char pecaac){
    int ld,le,lc,lb;
    int ldi, lds,lei,les;
    int legal=0;
    lb=verb(mat,l,c,pecaac);
    lc=verc(mat,l,c,pecaac);
    le=hore(mat,l,c,pecaac);
    ld=hord(mat,l,c,pecaac);
    ldi=diadi(mat,l,c,pecaac);
    lds=diads(mat,l,c,pecaac);
    lei=diaei(mat,l,c,pecaac);
    les=diaes(mat,l,c,pecaac);

    legal = lb+lc+le+ld+ldi+lds+lei+les;
    printf("\n %d",legal);
    if (legal==0){
        printf("Jogada ilegal");
    }
    else{
        return 1;
    }

}


int main(int argc, char *argv[])

{
    char matriz[7][7];
    char pecaj, pecaai;
    int x,y;

    pecaj='X';
    pecaai='O';
    inimatri(matriz);
    showmat(matriz);

    x=6;
    y=4;

    clegal(matriz,x,y,pecaj);


    return 0;
}

I tried to check if i was adding more numbers than the array can store but i dint find any problem, also searched for other solutions but couldnt find anything


Solution

  • In testing out your code as is on my Linux system, it indeed threw a smashing error. In reviewing your code, a couple of things seemed to stick out which seemed to contribute to ending with a smashing error.

    First off, as is pointed out in the good comments above, there seems to be some confusion as to when array elements are starting at "1" or "0", and also it appears that the program really is intended to work with an "8 x 8" array and not a "7 x 7" array.

    In reviewing the following "printf" statement, it is evident that an "8 x 8" array is intended to be used.

    printf("  0 1 2 3 4 5 6 7");
    

    So with that in mind, I did a bit of refactoring of the program to set up an "8 x 8" array along with utilizing ranges from "0" to "7" to conform to the usual and customary index subscripts within "C" programs. Following is the refactored version of your program.

    #include <stdio.h>
    #include <stdlib.h>
    
    void inimatri(char mat[8][8])   /* Going by the printout of rows and columns, it is intimated that there are eight rows and eight columns */
    {
        int l=0,c=0;
    
        while (l<8)
        {
            while (c<8)
            {
                mat[l][c]='.';
                c=c+1;
            }
            c=0;
            l=l+1;
        }
    
        mat[3][3]='O';
        mat[3][4]='X';
        mat[4][3]='X';
        mat[4][4]='O';
        mat[2][5]='O';
        mat[5][2]='O';
        mat[3][2]='O';
        mat[5][4]='O';
    }
    
    void showmat (char mat[8][8])
    {
    
        int l=0,c=0;
    
        printf("  0 1 2 3 4 5 6 7");
    
        while (l<8)
        {
            printf( "\n%d",l);
            while (c<8)
            {
                printf( " %c",mat[l][c]);
                c=c+1;
            }
            c=0;
            l=l+1;
        }
    }
    
    int hord(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (c== 8)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l][c+1]==pecaac)
        {
            return 0;
        }
        while ((c+1)<8)
        {
            if (mat[l][c+1]=='.')
            {
                return 0;
    
            }
            else if (mat[l][c+1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal= 0;
            }
            c=c+1;
        }
        return(legal);
    }
    
    int hore(char mat[8][8], int l, int c, char pecaac)
    {
        int legal=0;
    
        if (c== 0)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l][c-1]==pecaac)
        {
            return 0;
        }
        while (0<=(c-1))
        {
            if (mat[l][c-1]=='.')
            {
                return 0;
    
            }
            else if (mat[l][c-1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal=0;
            }
    
            c=c-1;
        }
        return (legal);
    }
    
    int verb(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 8)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l+1][c]==pecaac)
        {
            return 0;
        }
        while ((l+1)<8)
        {
            if (mat[l+1][c]=='.')
            {
                return 0;
    
            }
            else if (mat[l+1][c]== pecaac)
            {
                return 1;
            }
            else
            {
                legal=0;
            }
            l=l+1;
        }
        return (legal);
    }
    
    int verc(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 0)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l-1][c]==pecaac)
        {
            return 0;
        }
        while (0<=(l-1))
        {
            if (mat[l-1][c]=='.')
            {
                return 0;
    
            }
            else if (mat[l-1][c]== pecaac)
            {
                return 1;
            }
            else
            {
                legal = 0;
            }
            l=l-1;
        }
        return (legal);
    }
    
    int diads(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 0 || c== 8)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l-1][c+1]==pecaac)
        {
            return 0;
        }
        while (0<=(l-1) && c+1<8)
        {
            if (mat[l-1][c+1]=='.')
            {
                return 0;
    
            }
            else if (mat[l-1][c+1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal = 0;
            }
            l=l-1;
            c=c+1;
        }
        return (legal);
    }
    
    int diadi(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 8 || c== 8)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l+1][c+1]==pecaac)
        {
            return 0;
        }
        while ((l+1)<8 && (c+1)<8)
        {
            if (mat[l+1][c+1]=='.')
            {
                return 0;
    
            }
            else if (mat[l+1][c+1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal = 0;
            }
            l=l+1;
            c=c+1;
        }
        return (legal);
    }
    
    int diaes(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 0 || c== 0)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l-1][c-1]==pecaac)
        {
            return 0;
        }
        while (0<=(l-1) && 0<=(c-1))
        {
            if (mat[l-1][c-1]=='.')
            {
                return 0;
    
            }
            else if (mat[l-1][c-1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal = 0;
            }
            l=l-1;
            c=c-1;
        }
        return (legal);
    }
    
    int diaei(char mat[8][8], int l, int c, char pecaac)
    {
        int legal;
        if (l== 7 || c== 0)
        {
            return 0;
        }
        if (mat[l][c]!='.')
        {
            return 0;
        }
        if (mat[l+1][c-1]==pecaac)
        {
            return 0;
        }
        while ((l+1)<8 && 0<=(c-1))
        {
            if (mat[l+1][c-1]=='.')
            {
                return 0;
    
            }
            else if (mat[l+1][c-1]== pecaac)
            {
                return 1;
            }
            else
            {
                legal = 0;
            }
            l=l+1;
            c=c-1;
        }
        return (legal);
    }
    
    int clegal(char mat[8][8], int l, int c, char pecaac)
    {
        int ld,le,lc,lb;
        int ldi, lds,lei,les;
        int legal=0;
        lb=verb(mat,l,c,pecaac);
        lc=verc(mat,l,c,pecaac);
        le=hore(mat,l,c,pecaac);
        ld=hord(mat,l,c,pecaac);
        ldi=diadi(mat,l,c,pecaac);
        lds=diads(mat,l,c,pecaac);
        lei=diaei(mat,l,c,pecaac);
        les=diaes(mat,l,c,pecaac);
    
        legal = lb+lc+le+ld+ldi+lds+lei+les;
        printf("\nLegal: %d\n",legal);      /* Just to clarify what this value is when it is printed */
        if (legal==0)
        {
            printf("Jogada ilegal");
        }
        else
        {
            return 1;
        }
        return 0;   /* Added this to address a compiler warning regarding not returning an integer value if this point is reached */
    }
    
    int main(int argc, char *argv[])
    {
        char matriz[8][8];
        char pecaj, pecaai;     /* FYI - the compiler warned that variable pecaai is set but not used */
        int x,y;
    
        pecaj='X';
        pecaai='O';
        inimatri(matriz);
        showmat(matriz);
    
        x=6;
        y=4;
    
        clegal(matriz,x,y,pecaj);
    
        return 0;
    }
    

    Some key items to point out.

    With that, following is a sample output at the terminal on my Linux system.

        @Vera:~/C_Programs/Console/Smash/bin/Release$ ./Smash 
      0 1 2 3 4 5 6 7
    0 . . . . . . . .
    1 . . . . . . . .
    2 . . . . . O . .
    3 . . O O X . . .
    4 . . . X O . . .
    5 . . O . O . . .
    6 . . . . . . . .
    7 . . . . . . . .
    Legal: 1
    

    Review this refactored code at your leisure and see if it meets the spirit of your project.