2017-12-02 255 views
0
struct _StoryElement_ { 
char *title_of_chapter_; 
struct _StoryElement_ *path_a_; 
struct _StoryElement_ *path_b_; 
char *content_of_chapter_; 
}; 
typedef struct _StoryElement_ StoryElement; 

因此,我創建了一個Tree,其中每個Structs都包含不同的值。我初始化它們都使用以下功能:使用指針訪問結構成員的垃圾數據

StoryElement *insertIntoStoryElement(StoryElement* root, char* title_of_chapter, char* content_of_chapter) 
{ 
    if(root == NULL) 
    { 
    root = makeNewStoryElement(root, title_of_chapter, content_of_chapter); 
    } 
    else if (root->path_a_ == NULL) 
    { 
    root->path_a_ = makeNewStoryElement(root, title_of_chapter, content_of_chapter); 
    } 
    else if (root->path_b_ == NULL) 
    { 
    root->path_b_ = makeNewStoryElement(root, title_of_chapter, content_of_chapter); 
    } 

    return root; 
} 

StoryElement *makeNewStoryElement(StoryElement* root, char* title_of_chapter, 
            char* content_of_chapter) 
{ 
    root = (StoryElement*) malloc(sizeof(StoryElement)); 

    root->title_of_chapter_ = 
    (char*)malloc(sizeof(char*)*(strlen(title_of_chapter) + 1)); 
    root->content_of_chapter_ = 
    (char*)malloc(sizeof(char*)*(strlen(title_of_chapter) + 1)); 

    //strcpy(NewStoryElement->title_of_chapter_, title_of_chapter); 
    //strcpy(NewStoryElement->content_of_chapter_, content_of_chapter); 
    title_of_chapter = root->title_of_chapter_; 
    content_of_chapter = root->content_of_chapter_; 

    root->path_a_ = NULL; 
    root->path_b_ = NULL; 

    return root; 
} 

此功能就是爲我提供了我轉嫁給insertIntoStoryElement()的字符串值:

StoryElement *createStoryTree (StoryElement *root, char *storage) 
{ 

    char* pos = storage; 
    pos = strchr(pos, '\n'); 
    *pos = '\0'; 
    int size = strlen(storage); 
    char* title = malloc(size + 1); 
    strcpy(title, storage); 

    char* ptr_path_a = pos + 1; 
    pos = strchr(ptr_path_a, '\n'); 
    *pos = '\0'; 
    size = strlen(ptr_path_a); 
    char* path_a = malloc(size + 1); 
    strcpy(path_a, ptr_path_a); 

    char* ptr_path_b = pos + 1; 
    pos = strchr(ptr_path_b, '\n'); 
    *pos = '\0'; 
    size = strlen(ptr_path_b); 
    char* path_b = malloc(size + 1); 
    strcpy(path_b, ptr_path_b); 

    char* ptr_text = pos + 1; 
    pos = strchr(pos + 1, '\0'); 
    *pos = '\0'; 
    size = strlen(ptr_text); 
    char* text = malloc(size + 1); 
    strcpy(text, ptr_text); 

    root = insertIntoStoryElement(root, title, text); 

/* if(strcmp(path_a, "-")!=0 && strcmp(path_b, "-")!=0) 
    {*/ 
    root->path_a_ = readStoryFromFile(root->path_a_, path_a); 
    root->path_a_ = readStoryFromFile(root->path_b_, path_b); 
    //} 

    return root; 
} 

這是最終調用的函數主:

StoryElement *readStoryFromFile (StoryElement *root, char *filename) 
{ 
    if(strcmp(filename, "-") == 0) 
    { 
    //printf("End reached\n"); 
    return 0; 
    } 
    FILE *file = fopen(filename, "r"); 

    if(!file) 
    { 
    printf("[ERR] Could not read file %s.\n", filename); 
    return 0; 
    } 

    long fsize = getFileSize(file); 

    char* storage = malloc(fsize + 1); 
    if(!storage) 
    { 
    printf("[ERR] Out of memory.\n"); 
    return 0; 
    } 

    fread(storage, fsize, 1, file); 
    storage[fsize] = '\0'; 
    fclose(file); 

    root = createStoryTree(root, storage); 

    free(storage); 

    return root; 
} 

這是我的主要它使用另一種功能,僅高於2是相關的這個問題,我認爲:

int main (int argc, char *argv[]) 
{ 
    if (argc != 2) /* argc should be 2 for correct execution */ 
    { 
    printf("Usage: ./ass2 [file-name]\n"); 
    return (1); 
    } 

    StoryElement *root = NULL; 

    root = readStoryFromFile(root, argv[1]); 
    if(!root) 
    return 3; 

    printf("%p\n", root); 
    printf("%s\n", root->title_of_chapter_); 

    //printStoryTree(root); 
    freeStoryTree(root); 

    root = NULL; 

    return 0; 
} 

最後我的問題是,2個printfs輸出後,在主函數返回以下內容: First pointer Address is ok I think, but the second printf should be "Kapitel_1.txt" 我爲什麼在這裏得到一個垃圾的價值?這是分段錯誤嗎?

+0

由於錯誤發生在最令人意想不到的地方,我們希望看到'readStoryFromFile'並能夠跟着它,直到它返回(並打印錯誤的數據)。請提供。 –

+0

不鼓勵在名稱中使用尾部下劃線,應該爲系統和庫保留。 –

+0

爲什麼你註釋掉'makeNewStoryElement'中的兩個'strcpy'語句?事實上,現在它不會工作。 –

回答

0

在您的函數makeNewStoryElement中malloc爲root->title_of_chapter指定的空間,但是您不會將給定的title_of_chapter的內容放入其中。這意味着root->title_of_chapter將包含您從malloc返回的位置中的任何垃圾數據。
做一些類似於 strcpy(root->title_of_chapter, title_of_chapter);
printf將會很好。
該代碼還有其他一些問題和你不應該做的事情,但是這應該解決你所問的問題。

+0

我確實嘗試了strcpy()函數(註釋掉了這些函數但用「root-> title_of_chapter_」),但它導致另一個程序由於另一個錯誤而崩潰。可能來自createStoryTree函數。 – blablu