2017-05-12 2 views
1

マルコフチェーンを作成するプログラムを作成しようとしていますが、ポインタに問題があります。私がプログラムを実行すると、セグメンテーション違反が発生します。どのようにポインタエラーを突き止めますか?

#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; 
    } 

} 

ファイルの読み取り部分を削除して、ハードコードされた文字列に置き換えようとしましたが、問題は残りました。

+10

デバッガで実行しようとしましたが、クラッシュした行は何ですか? – bruceg

+3

デバッガの使い方がわからない場合は、print文を使用してクラッシュした箇所を確認してから、その部分を深く分析してください。 –

+5

これ以外のC++の唯一のものは、あなたがどのように記述するかです。目標がCプログラムの場合は、それらを変更してください(例:cstdlib => stdlib.h) – deviantfan

答えて

0

STLについて読んでリストを使用したいと思うかもしれません。または、例のカップルを見、Adding node in front of linklist How to pop element from tail in linked list? Trying to make linkedlist in C

いくつかの問題をCのリストを使用しています。一部を修正しました。コンパイルする。 あなたは、これは、コンパイルし、エラーなしで実行されます、私はあなたが境界チェックを修正する必要がある場所でコードを注釈を付けていると、大きな問題がありそうな構造体言葉の> wordstr初期化されていないのchar *へのstrcpyた、

#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 
} 

リンクリスト処理を調べる必要があります。リンクされたリストを実装し、リストに単語要素を追加する必要があります。

関連する問題