C:以seg错误结尾的Linked-list-iteration

时间:2013-09-14 20:01:44

标签: c linked-list segmentation-fault

我正在开发一个C程序,它读取文本文件,将每一行放在一个char数组中,并将该行存储在一个链表节点中。创建链表的过程似乎有效,但是当我调用我的函数时应该打印存储在链表中的文本而没有任何元音(不要问我为什么会这样!),我得到一个分段错误...打印完所有文本后没有任何元音。

LinkedList节点如下所示:

typedef struct linked_list_node link;

struct linked_list_node {
char* word;
link* next;
};

并且no-vowel函数如下所示:

void print_no_vowels(link* lines)
{
char vowels[] = "aeiouy";
unsigned int i = 0;
unsigned int j = 0;
unsigned int check = 0;
link* first = lines;

while(first != NULL)
{
    printf("%p\n", first);
    for(i = 0; i < strlen(first->word)-1; i++)
    {
        for(j = 0; j < strlen(vowels)-1; j++)
        {
            if(first->word[i] == vowels[j])
            {
                check = 1;
            }
        }
        if(check != 1)
        {
            printf("%c", first->word[i]);
        } else {
            check = 0;
        }
    }
    printf("\n");

    first = first->next;
}
}

经过多次打印之后,我得出的结论是,即使第一个指针指向NULL,程序也会再次进入print_no_vowels函数的while循环。我不知道为什么会发生这种情况 - 在我使用相同方式迭代链表的所有其他函数中,当第一次== NULL时,程序不会再次进入while循环。

感谢任何帮助,提前谢谢!


编辑:

这是链表的init函数。

link* read_file(FILE *input)
{
char line[100];
link *first = malloc(sizeof(link));
first->word = NULL;
link *curr = NULL;
while(fgets(line, 100, input) != NULL)
{
    if(first->word == NULL)
    {
        first->word = malloc(100);
        strcpy(first->word, line);
        first->next = malloc(sizeof(link));
        curr = first->next;
    }
    else
    {
        if(line != NULL)
        {
            curr->word = malloc(100);
            strcpy(curr->word, line);
            curr->next = malloc(sizeof(link));
            curr = curr->next;
        }
    }
}

return first;
}

在你这样说之前 - 我知道我是C的初学者。任何帮助都是真的很感激,我是个菜鸟:)

2 个答案:

答案 0 :(得分:1)

崩溃是因为您没有可靠地将新分配的next的{​​{1}}指针设置为NULL,但您的迭代依赖于它为NULL。在从link中读取字符之前,您还需要检查first->word中的print_no_vowels()是否为空。

另外,你不想要strlen(vowels)-1 - 它会留下y。

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

typedef struct linked_list_node link;

struct linked_list_node
{
    char *word;
    link *next;
};

static void print_no_vowels(link *lines)
{
    char vowels[] = "aeiouy";
    unsigned int i = 0;
    unsigned int j = 0;
    unsigned int check = 0;
    link *first = lines;

    while (first != NULL && first->word != NULL)
    {
        printf("%p: ", first);
        for (i = 0; i < strlen(first->word)-1; i++)
        {
            for (j = 0; j < strlen(vowels); j++)
            {
                if (first->word[i] == vowels[j])
                {
                    check = 1;
                }
            }
            if (check != 1)
            {
                printf("%c", first->word[i]);
            }
            else
            {
                check = 0;
            }
        }
        printf("\n");

        first = first->next;
    }
}

static link *read_file(FILE *input)
{
    char line[100];
    link *first = malloc(sizeof(link));
    first->word = NULL;
    first->next = NULL;
    link *curr = NULL;
    while (fgets(line, 100, input) != NULL)
    {
        if (first->word == NULL)
        {
            first->word = malloc(100);
            strcpy(first->word, line);
            first->next = malloc(sizeof(link));
            curr = first->next;
        }
        else
        {
            curr->word = malloc(100);
            strcpy(curr->word, line);
            curr->next = malloc(sizeof(link));
            curr = curr->next;
        }
        curr->next = NULL;
        curr->word = NULL;
    }

    return first;
}

int main(void)
{
    link *list = read_file(stdin);
    if (list != 0)
        print_no_vowels(list);
    return 0;
}

在源代码上运行程序产生:

0x7fe75b4000e0: #ncld <std.h>
0x7fe75b403a80: #ncld <stdlb.h>
0x7fe75b403b00: #ncld <strng.h>
0x7fe75b403b80: 
0x7fe75b403c00: tpdf strct lnkd_lst_nd lnk;
0x7fe75b403c80: 
0x7fe75b403d00: strct lnkd_lst_nd
0x7fe75b403d80: {
0x7fe75b403e00:     chr *wrd;
0x7fe75b403e80:     lnk *nxt;
0x7fe75b403f00: };
0x7fe75b403f80: 
0x7fe75b404000: sttc vd prnt_n_vwls(lnk *lns)
0x7fe75b404080: {
0x7fe75b404100:     chr vwls[] = "";
0x7fe75b404180:     nsgnd nt  = 0;
0x7fe75b404200:     nsgnd nt j = 0;
0x7fe75b404280:     nsgnd nt chck = 0;
0x7fe75b404300:     lnk *frst = lns;
0x7fe75b404380: 
0x7fe75b404400:     whl (frst != NULL && frst->wrd != NULL)
0x7fe75b404480:     {
0x7fe75b404500:         prntf("%p: ", frst);
0x7fe75b404580:         fr ( = 0;  < strln(frst->wrd)-1; ++)
0x7fe75b404600:         {
0x7fe75b404680:             fr (j = 0; j < strln(vwls); j++)
0x7fe75b404700:             {
0x7fe75b404780:                 f (frst->wrd[] == vwls[j])
0x7fe75b404800:                 {
0x7fe75b404880:                     chck = 1;
0x7fe75b404900:                 }
0x7fe75b404980:             }
0x7fe75b404a00:             f (chck != 1)
0x7fe75b404a80:             {
0x7fe75b404b00:                 prntf("%c", frst->wrd[]);
0x7fe75b404b80:             }
0x7fe75b404c00:             ls
0x7fe75b404c80:             {
0x7fe75b404d00:                 chck = 0;
0x7fe75b404d80:             }
0x7fe75b404e00:         }
0x7fe75b404e80:         prntf("\n");
0x7fe75b404f00: 
0x7fe75b404f80:         frst = frst->nxt;
0x7fe75b405000:     }
0x7fe75b405080: }
0x7fe75b405100: 
0x7fe75b405180: sttc lnk *rd_fl(FILE *npt)
0x7fe75b405200: {
0x7fe75b405280:     chr ln[100];
0x7fe75b405300:     lnk *frst = mllc(szf(lnk));
0x7fe75b405380:     frst->wrd = NULL;
0x7fe75b405400:     frst->nxt = NULL;
0x7fe75b405480:     lnk *crr = NULL;
0x7fe75b405500:     whl (fgts(ln, 100, npt) != NULL)
0x7fe75b405580:     {
0x7fe75b405600:         f (frst->wrd == NULL)
0x7fe75b405680:         {
0x7fe75b405700:             frst->wrd = mllc(100);
0x7fe75b405780:             strcp(frst->wrd, ln);
0x7fe75b405800:             frst->nxt = mllc(szf(lnk));
0x7fe75b405880:             crr = frst->nxt;
0x7fe75b405900:         }
0x7fe75b405980:         ls
0x7fe75b405a00:         {
0x7fe75b405a80:             crr->wrd = mllc(100);
0x7fe75b405b00:             strcp(crr->wrd, ln);
0x7fe75b405b80:             crr->nxt = mllc(szf(lnk));
0x7fe75b405c00:             crr = crr->nxt;
0x7fe75b405c80:         }
0x7fe75b405d00:         crr->nxt = NULL;
0x7fe75b405d80:         crr->wrd = NULL;
0x7fe75b405e00:     }
0x7fe75b405e80: 
0x7fe75b405f00:     rtrn frst;
0x7fe75b405f80: }
0x7fe75b406000: 
0x7fe75b406080: nt mn(vd)
0x7fe75b406100: {
0x7fe75b406180:     lnk *lst = rd_fl(stdn);
0x7fe75b406200:     f (lst != 0)
0x7fe75b406280:         prnt_n_vwls(lst);
0x7fe75b406300:     rtrn 0;
0x7fe75b406380: }

代码中仍有很大的改进空间,但至少它没有崩溃。除此之外,您还需要将代码编写为免费列表。它仍应修改为使用strdup()

答案 1 :(得分:0)

for(i = 0; i < strlen(first->word)-1; i++)
    {
        for(j = 0; j < strlen(vowels)-1; j++)

你不想使用strlen() - 1而只是使用strlen。来自的空字符串 一个空行将下溢i到0xFFFFFFFF(如果你在32位架构上运行)并导致你访问word中的无效内存

编辑:您还需要将列表的最后一个元素初始化为NULL

 curr = first->next;
 curr->next = NULL;

curr = curr->next;
curr->next = NULL;