c++ - 如何定位指针错误?

标签 c++ pointers struct

我正在尝试创建一个程序来创建马尔可夫链,但我遇到了指针问题。当我运行该程序时,出现段错误。

#include <stdio.h>
#include <cstring>
#include <cstdlib>
struct word;
struct nextword
{
    word* sourceword;
    word* next = 0;
};
int wordcount;
struct word
{
    char* wordstr;
    struct word* next = 0;
    nextword* followingword = 0;
    int nextwordcount = 0;
};
int main()
{
    word* firstword = 0;
    char * buffer = 0;
    long length;
    FILE * f = fopen ("alice.txt", "rb");

    if (f)
    {
        fseek (f, 0, SEEK_END);
        length = ftell (f);
        fseek (f, 0, SEEK_SET);
        buffer = (char *)malloc (length);
        if (buffer)
        {
            fread (buffer, 1, length, f);
        }
        fclose (f);
    }

    if (buffer)
    {
        char wordbuffer[500];
        int fileindex = 0;
        while(fileindex < length-1)
        {
            int wordindex = 0;
            while(buffer[fileindex] != ' ')
            {
                    wordbuffer[wordindex] = buffer[fileindex];
                    wordindex++;
                    fileindex++;
            }
            if(wordindex != 0)
                {
                    wordbuffer[wordindex] = '\0';
                    word* newword = (word*)malloc(sizeof(word));
                    char* newwordstr = (char*)malloc((strlen(wordbuffer)+1)*sizeof(char));
                    strcpy(newword->wordstr, newwordstr);
                    if(!firstword)
                {
                    firstword = newword;
                }
                    else
                {
                    word* testword = firstword;
                    while(!testword->next)
                        {
                            testword = (testword->next);
                        }
                    testword->next = newword;
                    printf(newword->wordstr);
                }
                }

            return 0;
        }
    }
    else
    {
            return 1;
    }

}

我试图删除文件读取部分并用硬编码字符串替换它,但问题仍然存在。

最佳答案

您可能想阅读有关 STL 的内容并使用列表。或者使用 C 列表,看几个例子, Adding node in front of linklist How to pop element from tail in linked list? Trying to make linkedlist in C

几个问题。修复了一些。编译。 我已经在需要修复边界检查的地方对代码进行了注释,最大的问题可能是 strcpy 到 struct word->wordstr uninitialized char*,

#include <stdio.h>
#include <cstring>
#include <cstdlib>
struct word;
struct nextword
{
    word* sourceword;
    word* next = 0;
};
int wordcount;
struct word
{
    char* wordstr; //what do you think this pointer points to?
    struct word* next = 0;
    nextword* followingword = 0;
    int nextwordcount = 0;
};
int main()
{
    FILE* fh = NULL;
    word* firstword = 0;
    char* buffer = 0;
    char* fname = "alice.txt";
    long length = 0; //you did not initialize length

    if ( (fh =  fopen ("alice.txt", "rb")) )
    {
        //why not use fstat to get file size?
        //why not use mmap to read file?
        fseek (fh, 0, SEEK_END);
        length = ftell (fh); //ok, length set here
        fseek (fh, 0, SEEK_SET);
        if( (buffer = (char *)malloc (length)) )
        {
            fread (buffer, 1, length, fh);
        }
        fclose (fh);
    }
    else
    {
        printf("error: cannot open %s",fname);
        exit(1);
    }
    printf("read %s, %ld\n",fname,length);

    if (!buffer)
    {
        printf("error: cannot open %s",fname);
        exit(1);
        //use exit, to return from main() //return 1;
    }

    //already checked buffer
    {
        int fileindex = 0;
        //put wordbuffer after fileindex, avoids stackoverflow overwrite
        char wordbuffer[500]; //500 bytes on stack, initialize?
        memset(wordbuffer,0,sizeof(wordbuffer));
        while(fileindex < length-1)
        {
            int wordindex = 0;
            //several errors in this line, check for null terminator,
            //check for newline, tab, basically any whitespace
            //while(buffer[fileindex] != ' ')
            while( buffer[fileindex] && buffer[fileindex] != ' ' )
            {
                wordbuffer[wordindex] = buffer[fileindex];
                wordindex++;
                fileindex++;
                //here is another error, do not overflow your stack based buffer
                if( wordindex>sizeof(buffer)-1 ) break; //do not overflow buffer
            }
            wordbuffer[wordindex] = '\0'; //terminate wordbuffer
            //since you chose wordindex signed, you want it > 0
            if(wordindex > 0)
            {
                //use a constructor
                word* newword = (word*)malloc(sizeof(word));
                //use a constructor
                //or just use strdup, since it is just a cstring
                char* newwordstr = strdup(wordbuffer);
                //no, just set pointer to the above allocated string
                //strcpy(newword->wordstr, newwordstr);
                newword->wordstr = newwordstr;
                if(!firstword)
                {
                    firstword = newword;
                }
                else
                {
                    word* testword = firstword;
                    while(!testword->next)
                    {
                        testword = (testword->next);
                    }
                    testword->next = newword;
                    printf(newword->wordstr);
                }
            }
            return 0;
        }
    }
    exit(0); //done
}

这样编译运行没有报错,需要查链表处理。你应该实现一个链表,然后将单词元素添加到列表中。

关于c++ - 如何定位指针错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43927495/

相关文章:

c++ - 替代关键字表示

c++ - 用户重复下单价格不累积

c++ - openGL 着色器 : two object and one line to connect them

将一个 C 结构转换为另一个

swift - 在 iOS Swift 3.1 中不知道该类型时如何在 Struct 对象中设置值?

c++ - constexpr 可变参数模板和解包 std::array

在链表中使用指向结构的指针时出现 C 段错误

C++:传递数组与传递数组指针一样吗?

c++ - 我们可以在将对象转换为 (char *) 后取回对象吗

c++ - 指向结构或类的指针与指向第一个字段的指针