c - 在 C 中的结构内初始化 char* 值

标签 c pointers struct

我一直在网上寻找,但到目前为止,没有任何东西可以帮助我理解我的问题。目前我正在尝试制作两个列表,每个列表都包含一个单词列表。每个空格包含一个单词结构,每个单词结构内是 30 个 doc_list 结构的列表。我已经能够分配内存并将 char* 存储在单词列表结构中,但是当我尝试分配内存并将 char* 存储在 doc_list 结构中时,出现段错误。我很困惑,因为我声明我的 doc_list 结构的方式与我声明我的 word_list 结构的方式完全相同。

下面是我的结构体的初始化

在我的 C 文件中我初始化了我的哈希表

#define BUFFSIZE 1000
#define STOPLIST_INDEX 0
#define DOCUMENTS_INDEX 1

struct HashTable{
    int tableSize;
    struct word_list** wordList;
};

struct word_list{
    char* word;
    struct doc_list** docList;
};

struct doc_list{
   char* docName;
   int timesAppear;
};
//Initialing lists associated with hash table
struct HashTable** initialize_hash_table(int argc, char** argv)
{
    int k; //HashTables
    int i; //Words
    int q; //Document names
    struct HashTable** hashTable = calloc(2, sizeof(struct HashTable **));
    for(k =0; k < 2; k++)
    {
     hashTable[k] =  calloc (1, sizeof(struct HashTable *)); 
     hashTable[k]->wordList =  calloc(BUFFSIZE, sizeof(struct word_list **));

     for(i = 0; i < BUFFSIZE; i++)
     {
         hashTable[k]->wordList[i] =  calloc(1, sizeof(struct word_list *)); 
         hashTable[k]->wordList[i]->docList =  calloc(30, sizeof(struct doc_list**));
         for(q = 0; q < 30; q++)
         {
             hashTable[k]->wordList[i]->docList[q] = calloc(1, sizeof(struct doc_list*));
         }
     }
   } 


  return hashTable;
} 

然后在我存储文档名称 char* 值的插入函数中出现段错误。我不明白为什么会这样,因为我初始化 doc_list 结构的方式与初始化 word_list 结构的方式完全相同。

int insert(struct HashTable** hashTable, char* document_word, char* filename, int index)
{
    //create the hash key
    int key = hashFunction(document_word, BUFFSIZE);
    //Check if word exists in Stop List
    if(index == 0 || hashTable[STOPLIST_INDEX]->wordList[key]->word == NULL)
    {
        //insert into list
        hashTable[index]->wordList[key] = malloc(sizeof(struct word_list*));
        hashTable[index]->wordList[key]->word = strdup(document_word);
        printf("%s%s\n", "INSERTED VALUE: ", hashTable[index]->wordList[key]->word);
        //Add filename to words' document list

         int w = 0;
         puts("segfaulting here");
         puts("1");
         hashTable[index]->wordList[key]->docList[w] = malloc(sizeof(struct doc_list*));
         hashTable[index]->wordList[key]->docList[w]->docName = strdup(filename);
         printf("%s%s\n", "INSERTED ", filename);
         printf("\n"); 
    } 
    return 0;
}

我认为正在发生的是,如果没有为单词分配内存,则所有单词结构都被声明为 NULL,但由于某种原因没有一个 doc_list 结构被声明为 NULL。我为它们分配内存的方式与为 word_list 结构分配内存的方式完全相同。

程序运行时的输出是这样的:

Hashing filename:stopwords.txt
---------
INSERTED VALUE: a
segfaulting here
1
Segmentation fault (core dumped)

我做错了什么??

最佳答案

我看到的问题:

 hashTable[k] =  calloc (1, sizeof(struct HashTable *)); 

需要改成

 hashTable[k] =  calloc (1, sizeof(struct HashTable));

hasTable[k] 的类型是struct HashTable*。它指向的对象的大小必须是 struct HashTable,而不是 struct HashTable*

也有类似的错误:

struct HashTable** hashTable = calloc(2, sizeof(struct HashTable **));

它需要是:

struct HashTable** hashTable = calloc(2, sizeof(struct HashTable*));

您不会轻易看到错误,因为在大多数情况下 sizeof(struct HashTable*) 等于 sizeof(struct HashTable**)

initialize_hash_table 中有更多此类错误。

hashTable[k]->wordList[i]->docList =  calloc(30, sizeof(struct doc_list**));

需要

hashTable[k]->wordList[i]->docList =  calloc(30, sizeof(struct doc_list*));

hashTable[k]->wordList[i]->docList[q] = calloc(1, sizeof(struct doc_list*));

需要

hashTable[k]->wordList[i]->docList[q] = calloc(1, sizeof(struct doc_list));

您在insert 中有类似的错误。

hashTable[index]->wordList[key] = malloc(sizeof(struct word_list*));

需要

hashTable[index]->wordList[key] = malloc(sizeof(struct word_list));

hashTable[index]->wordList[key]->docList[w] = malloc(sizeof(struct doc_list*));

需要

hashTable[index]->wordList[key]->docList[w] = malloc(sizeof(struct doc_list));

关于c - 在 C 中的结构内初始化 char* 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306092/

相关文章:

c - 如何使用按位运算符连接 2 个数字,同时保持变量的初始值不变?

c - C 中的内存分配和指针

c - 所有结构标识符都自动向前声明

struct - Golang 在结构之间切换

Char* 在 malloc 之后返回垃圾值?

c - 从代码库中删除/应用 ifdef/else 的工具

c - 嵌入式处理器的快速斜边算法?

c - 使用指针变量 typedef

c - 为什么我的程序执行完了所有的操作,最后还是崩溃了?

c - 如果它在自己的定义中使用,我是否必须预定义类型/结构?