簡體   English   中英

分段錯誤 c strcmp 最后比較

[英]Segmentation fault c strcmp at last comparaison

我目前正在嘗試將char**中的每個字符串與另一個字符串進行比較。 為此,我嘗試使用strcmp ,但出現分段錯誤。 然后我嘗試使用手工制作的 strcmp,通過測試我已經能夠發現我的 seg。 當我發現的單詞和 char[i] 之間的比較結束時,就會發生錯誤。

這是一些行和一些測試,因此您可以解決問題。

這是我的主要內容:

int
main(int argc, char *argv[])
{
    char search[1024];

    ssize_t sz = getxattr(argv[1], "user.tag", search, sizeof(search) - 1);

    printf("Old tags: %s\n", search);

    if (sz != -1) {
        char **tab = malloc(sizeof(char **));

        tab = StringToTab(search);
        char *mot = malloc(sizeof(char));

        strcat(mot, argv[2]);
        strcat(mot, "\0");
        printf("\n%s\n", tab[3]);
        if (estDansLeTableau(tab, mot, nb_occ(search, ',') + 1)) {
            char *newtag = TabToString(removeFromTab(tab, argv[2],
                    (nb_occ(search, ','))
                ),
                (nb_occ(search, ','))
                );

            printf("\nICI 1\n");
            if (setxattr(argv[1], "user.tag", newtag, strlen(newtag), 0) == -1) {
                perror("ERREUR");
            }
            else {
                printf("\n La nouvelle liste de tag pour votre fichier est :  %s \n", newtag);
            }
            memset(newtag, 0, 0);
        }
        else {
            printf("\nle tag demandé n'est pas dans la liste des tags de ce fichier\n");
        }
        memset(tab, 0, 0);
    }
    else {
        printf("\nLe fichier n'a aucun tag a enlever\n");
    }
}

Tab 來自StringToTab函數,它基本上采用[abc,bcd,def]之類的字符串並將其轉換為["abc","bcd","def"]類的char**

char **
StringToTab(char *s)
{
    char **tab = malloc((nb_occ(s, ',') + 1) * sizeof(char *));
    char *add = malloc(sizeof(char *));
    char c = s[1];
    int j = 1;
    int i = 0;

    while (c != ']') {
        if (c == ',') {
            char *caracterVide = malloc(sizeof(&c));

            caracterVide = "\0";
            strcat(add, caracterVide);
            memset(caracterVide, 0, 0);
            printf("\nMot = %s\n", add);
            tab[i] = malloc(sizeof(char *));
            memcpy(tab[i], add, strlen(add));
            printf("\nAffected word = %s\n", tab[i]);
            memset(add, 0, sizeof(add));
            i++;
            j++;
            c = s[j];
        }
        else {
            char *sac = malloc(sizeof(&c));

            *sac = c;
            strcat(add, sac);
            memset(sac, 0, 0);
            printf("\nprefix = %s", add);
            j++;
            c = s[j];
        }
    }
    printf("\nMot = %s\n", add);
    tab[i] = malloc(sizeof(char *));
    memcpy(tab[i], add, strlen(add));
    printf("\nAffected word = %s\n", tab[i]);
    memset(add, 0, 0);
    i++;
    j++;
    c = s[j];
    return tab;
}

然后我們將 go 轉移到estDansLeTableau (=isInTheTab) ,它將獲取我們剛剛擁有的標簽,即我需要發現的標簽argv[2]

int
estDansLeTableau(char **t, char *m, int t_len)
{
    // 1 si le mot est dans le tableau, 0 sinon
    int j = 0;

    while (j < t_len) {
        printf("\nword 1 = %s, word 2 = %s\n", t[j], m);
        if (strcmp(t[j], m) == 0) {
            return 1;
        }
        else {
            j++;
        }
    }
    return 0;
}

這是一個測試,我試圖在標簽上發現“lol”:

舊標簽:[Amine,Chris,Sara,Kamelia,lol,Sarah,Sarah]

prefix = A
prefix = Am
prefix = Ami
prefix = Amin
prefix = Amine
Mot = Amine

Affected word = Amine

prefix = C
prefix = Ch
prefix = Chr
prefix = Chri
prefix = Chris
Mot = Chris

Affected word = Chris

prefix = S
prefix = Sa
prefix = Sar
prefix = Sara
Mot = Sara

Affected word = Sara

prefix = K
prefix = Ka
prefix = Kam
prefix = Kame
prefix = Kamel
prefix = Kameli
prefix = Kamelia
Mot = Kamelia

Affected word = Kamelia

prefix = l
prefix = lo
prefix = lol
Mot = lol

Affected word = lol

prefix = S
prefix = Sa
prefix = Sar
prefix = Sara
prefix = Sarah
Mot = Sarah

Affected word = Sarah

prefix = S
prefix = Sa
prefix = Sar
prefix = Sara
prefix = Sarah
Mot = Sarah

Affected word = Sarah

Kamelia

word 1 = Amine, word 2 = lol

word 1 = Chris, word 2 = lol

word 1 = Sara, word 2 = lol

word 1 = Kamelia, word 2 = lol

word 1 = lol, word 2 = lol
Segmentation fault

我被困在上面已經兩天了,我不知道如何解決它 SOS lmao! 非常感謝 !

大多數 memory 分配都是針對指針大小的。 在我的系統上是 8 個字節。 只要沒有超過七個字符的元素,它就可以工作。

char *caracterVide = malloc(sizeof(&c));
char *add = malloc(sizeof(char *));  

還有 memory 泄漏。

char **tab = malloc(sizeof(char **));
tab = StringToTab(search);

malloc 之后的賦值覆蓋了malloc返回的值,並且 memory 不能被釋放。

這很好奇。

char *sac = malloc(sizeof(&c));
*sac = c;
strcat(add, sac);

沒有什么可以確保sac是零終止的。 串聯可以純偶然地添加一個字符,或者它可以迭代直到找到一個零並可能使程序崩潰。

這是獲取原始字符串並將其分解為以逗號分隔的項目的一種方法。
strspnstrcspn對於計數字符非常有用。 strspn在第一個不匹配字符或終止零處停止計數,而strcspn在第一個匹配字符或終止零處停止計數。
這不會逐個字符地顯示項目,但可以這樣做。

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

char ** StringToTab(char *s) {
    char **tab = NULL;
    char *comma = s;
    char *item = s;
    int count = 0;
    int span = 0;

    while ( *item) {// loop to terminating zero
        item += strcspn ( item, ",");//count of characters that are not ,
        if ( *item) {
            ++item;//advance past ,
        }
        ++count;//add one for each comma
    }
    ++count;//add one for item fillowing last comma
    //allocate extra sentinel pointer
    if ( NULL == ( tab = calloc ( sizeof *tab, ( count + 1)))) {
        fprintf ( stderr, "calloc problem\n");
        return tab;
    }
    count = 0;

    item = s;
    item += strspn ( s, "[");//count of characters that are [

    while ( *item) {//not at terminating zero
        comma = item;//set comma to the same as item
        comma += strcspn ( item, ",");//count of characters that are not ,
        if ( *comma) {
            span = comma - item;
        }
        else {//at the terminating zero
            span = strcspn ( item, "]");//count of characters that are not ]
        }
        //allocate memory for each item
        if ( NULL == ( tab[count] = malloc ( span + 1))) {//span characters plus terminating zero
            fprintf ( stderr, "malloc problem\n");
            return tab;
        }
        strncpy ( tab[count], item, span);//copy span characters
        tab[count][span] = 0;//set terminating zero
        ++count;
        item = comma;
        if ( *comma) {//not terminating zero
            ++item;//increment past ,
        }
    }
    return tab;
}

int estDansLeTableau(char **t, char *m) {
    // 1 si le mot est dans le tableau, 0 sinon
    int j = 0;

    while ( t[j]) {
        printf("\nword 1 = %s, word 2 = %s\n", t[j], m);
        if (strcmp(t[j], m) == 0) {
            return j;
        }
        j++;
    }
    return -1;
}

int main(int argc, char *argv[]) {
    char **tab = NULL;
    char test[] = "[Amine,Chris,Sara,Kamelia,lol,Sarah,Sarah]";
    char mot[] = "lol";
    int each = 0;
    int match = 0;

    printf("Old tags: %s\n", test);

    tab = StringToTab ( test);
    if ( tab) {
        if ( 0 <= ( match = estDansLeTableau(tab, mot))) {
            printf("match %s at %d\n", mot, match);
        }

        each = 0;
        while ( tab[each]) {
            free ( tab[each]);
            ++each;
        }
        free ( tab);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM