1

Hey guys im working on this project. It wants me to find anagrams. My code works when im writing like 2 sentences. But it shows me a segmentation fault if i write more sentences then 5. I tried to find the problem but i cant figure it out. can some one check it for me? here is my code. Thanks alot!

int klinkercheck(char z)
{
    if (z =='a')
        return 1000;
    else if (z == 'e')
        return 10000;
    else if (z == 'i')
        return 100000;
    else if (z == 'o')
        return 1000000;
    else if (z == 'u')
        return 10000000;
    else if (z == 10)
        return 0;
    else if (z == '.' || z== ' ')
        return 0;
    else
        return 1;
}

int main()
{
    int a,k,i,m,l,*tablecost,*testcost;
    int **table, **test, **result;
    char character;

    scanf("%d",&a);
    table = (int **)malloc(sizeof(int)*a);
    tablecost = (int *)malloc(sizeof(int)*a);

    for(k=0; k<a; k++) {
        table[k]= (int *)malloc(sizeof(int)*26);
        /*printf("tablecost = %d",tablecost[k]);*/

    }


    for(k=0; k<a; k++) {

        for(i=0; i<2; i=i) {
            scanf("%c",&character);
            tablecost[k]= tablecost[k]+klinkercheck(character);
            if(character=='.') {
                /*printf("%d\n",k);*/
                break;
            } else if(character < 97) {
                /* do nothing*/
            } else {
                table[k][character%97]++;
            }
        }
    }

    scanf("%d",&m);
    test = (int **)malloc(sizeof(int)*m);
    result = (int **)malloc(sizeof(int)*m);
    testcost = (int *)malloc(sizeof(int)*m);

    for(k=0; k<m; k++) {
        test[k]= (int *)malloc(sizeof(int)*26);
        result[k] = (int *)malloc(sizeof(int)*a);

    }

    for (k = 0 ; k < m ; k++) {
        testcost[k]=0;
        for(i=0; i<20; i=i) {
            scanf("%c",&character);
            testcost[k] =testcost[k]+klinkercheck(character);
            if(character=='.') {
                break;
            } else if(character < 97) {
                /* do nothing*/
            } else {
                test[k][character%97]++;
            }
        }
    }

    for (i = 0 ; i < m ; i++) {
        for (k = 0 ; k < a ; k++) {
            if (testcost[i] == tablecost[k]) {
                for (l = 0 ; l < 26 ; l++) {
                    if (test[i][l] != table[k][l]) {
                        break;
                    } else if (l == 25) {
                        printf("%d ", k + 1);
                    }
                }
            }
        }
        printf("\n");
    }

    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
A.Z. Amiri
  • 11
  • 1
  • 6
    TL;DR! Start by using a debugger to see *where* the crash happens (in your code), then edit your question to only include the relevant parts (together with the values of involved variables). – Some programmer dude Feb 11 '15 at 13:49
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Also remember that `malloc()` can fail. – unwind Feb 11 '15 at 13:50
  • 1
    Though, an even better start would be to [not cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc), and actually check that you allocate correctly (`sizeof(int)` is not always equal to `sizeof(int *)`). – Some programmer dude Feb 11 '15 at 13:50
  • I am assuming that this was a typo: "for(i=0;i<20;i=i){" this for loop would never end as i would never change from 0. – Rob Feb 11 '15 at 13:52

2 Answers2

1
table = (int **)malloc(sizeof(int)*a);

should be

table = malloc(sizeof(int *)*a);

and also

test = malloc(sizeof(int *)*m);
result = malloc(sizeof(int *)*m);

Dont cast malloc()

Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • It might me the case that `sizeof(int) == sizeof(int *)`. But it's reasonable to think that this is the issue. – Iharob Al Asimi Feb 11 '15 at 14:02
  • Also Gopi, please give the OP a link to the most popular c question on SO, Do I cast ... you know which one, don't you? – Iharob Al Asimi Feb 11 '15 at 14:03
  • @iharob Yeah but what we need to `sizeof(int *)` and since the standard never guarantees `sizeof(int) == sizeof(int *)` we should have `sizeof(int*)` Yes ofcourse I will add the popular link – Gopi Feb 11 '15 at 14:04
  • Oh, and apparently you should mention `scanf("%c",&character);` it would be good to have a space `" %c"` in the third `scanf()`. – Iharob Al Asimi Feb 11 '15 at 14:05
  • And I don't argue to that, alwaus use `-1` star in the `sizeof()` when `malloc()`ing – Iharob Al Asimi Feb 11 '15 at 14:06
  • I finaly got it working thanks alot for the tips it really helped. And the infinite loop was on purpose. – A.Z. Amiri Feb 11 '15 at 14:17
0

Unless sizeof(int)==sizeof(int*) on your platform the line:

table = (int **)malloc(sizeof(int)*a);

Will cause trouble. Use:

table = malloc(sizeof(*table)*a);

NB: Old boys get ventilated about casting the return of malloc().

Persixty
  • 8,165
  • 2
  • 13
  • 35