我找不到导致间歇性崩溃的指针错误。你能吗?

时间:2015-06-01 22:00:48

标签: c pointers linked-list

这大部分时间都有效,但我偶尔会崩溃。某处有一个指针问题,但我还没有看到它。

代码从字符串中获取单词,并构建它们的链接列表。单词必须包括相邻的标点符号,但不包括空格。例如,字符串:

  他说,'等等等等!'然后就死了。

将成为字符串

  

     

表示,

     

“等等

     

嗒嗒!'

     

     

然后

     

死亡。

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

unsigned longestword(char *str);

typedef struct _list{
    char *p;
    struct _list *next;
}list;


int main(void){
    char str[]="   'Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs!'";
    unsigned k, i=0, l, j, wordscout;
    list *curr, *root=NULL;

    k =longestword(str);
    puts(str);
    printf("\nlongest word in string is %u letters long.", k);

    do{
        //  skip over any leading whitespace.
        for(; str[i] && isspace(str[i]); i++);
        if(!str[i]) break;

        //  count length of word that begins with str[i].
        for(wordscout=i, l=0; str[wordscout] && !isspace(str[wordscout]); wordscout++, l++);

        //  if this is first word, malloc space to root.
        if(root==NULL){
            if((root = malloc(sizeof(list))) == NULL){
                printf("\nmalloc() failed.");
                exit(1);
            }
            curr = root;
        }

        //  if not first word, malloc space to curr->next.
        else{
            if((curr->next = malloc(sizeof(list))) == NULL){
                printf("\nmalloc() failed.");
                exit(1);
            }
            curr = curr->next;
        }

        //  malloc space in current struct for string.
        if((curr->p = malloc(1+l*sizeof(char))) == NULL){
            printf("\nmalloc() failed.");
            exit(1);
        }

        //  read first word into new space.
        j=0;
        while(!isspace(str[i])) curr->p[j++] = str[i++];
        curr->p[j] = '\0';

        //  check if word is there.
        printf("\n<<%s>>", curr->p);
    }while(str[wordscout]);
}


//  takes a null-terminated string, returns length of longest word in the string. word includes adjacent punctuation, but not whitespace.
unsigned longestword(char *str){

    //  check that word is null-terminated before carrying on.
    unsigned j,k,i,l;
    l = strlen(str);
    for(i=j=k=0; i<=l; i++){
        if(isalpha(str[i]) || ispunct(str[i])) j++;
        else if(j>k){ k=j; j=0; }
        else j=0;
    }
    return k;
}

可以忽略longestword()函数。它可以工作,以后它可以用于别的东西。

我的输出如下,这就是我想要的。但是一次又一次,它在显示之后崩溃了:

   'Well!' thought Alice to herself, 'after such a fall as this, I shall think n
othing of tumbling down stairs!'

longest word in string is 8 letters long.
<<'Well!'>>
<<thought>>
<<Alice>>
<<to>>
<<herself,>>
<<'after>>
<<such>>
<<a>>
<<fall>>
<<as>>
<<this,>>
<<I>>
<<shall>>
<<think>>
<<nothing>>
<<of>>
<<tumbling>>
<<down>>
<<stairs!'>>
Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.

2 个答案:

答案 0 :(得分:2)

如果字符串不以空格结尾,

while(!isspace(str[i]))将永远不会终止。

段错误可能取决于字符串结束后垃圾空白出现之前需要多长时间。

相反,你可以做while( i < wordscout )。请注意,l是多余的,您应该使用wordscout - i

答案 1 :(得分:0)

你应该在某处将NULL设置为curr-&gt;。

curr->next = malloc()是不够的,因为malloc()不会将next初始化为NULL指针。列表中的最后条目将curr->next指向随机位置。一旦你去那个地方,你的程序就会崩溃。

如果您不理解上述内容,请参阅以下内容:

将以下功能添加到您的程序中:

void foo()
{
    /* Fill the heap with garbage */
    list ** p = malloc(sizeof(list*)*8192);
    int i;
    for(i=0;i<8192;++i)
    {
       p[i]=malloc(sizeof(list));
       p[i]->next=p[i]; /*just something*/
    }
    for(i=0;i<8192;++i)
    {
       free( p[i] );
    }
    free(p);
}

在填写列表之前运行它:

int main(void){
    foo(); <<-- my new line
    char str[]="......

打印最后一个指针:

    }while(str[wordscout]);

    printf("%p\n", curr->next); <<--my new line

    return 0;

以下是打印出来的内容:

...
<<tumbling>>
<<down>>
<<stairs!'>>0x1f61240 <<-- non-null here!

这意味着当您导航列表时,您没有正确的END标记。无论谁使用你的清单,他们都不知道他们何时到达最后一个元素。

最好的修复方法是设置curr-&gt;然后设置curr-&gt; p:

//  malloc space in current struct for string.
if((curr->p = malloc(1+l*sizeof(char))) == NULL){
    printf("\nmalloc() failed.");
    exit(1);
}

// At this moment curr->next should be NULL
curr->next=NULL;
相关问题