简体   繁体   English

分段错误(核心转储)错误

[英]Segmentation fault (core dumped) error

I'm writing a program that uses a Hashtable of linked lists to count the frequency of words.我正在编写一个程序,它使用链表的哈希表来计算单词的频率。 The program will count all the words that I enter along with the frequency however after printing the hashtable, I get a segmentation fault (core dumped) error.该程序将计算我输入的所有单词以及频率但是在打印哈希表后,我得到一个分段错误(核心转储)错误。 When I valgrind my program, it shows that I get errors in three different places that are Invalid read of size 8. I'm not sure how to fix them though.当我对我的程序进行 valgrind 时,它表明我在三个不同的地方遇到错误,这些错误是大小为 8 的无效读取。不过,我不确定如何修复它们。 Here are the three different places:以下是三个不同的地方:

 void freeTable(HashTablePtr table) {
     int i;
     ListPtr list;

     if (table == NULL)
         return;

     for (i = 0; i < table->size; i++) {
         list = table->table[i];
         freeList(list);
     }

     free(table->table);
     free(table);
 }


 HashTablePtr createTable(int tableSize) {

     int i;
     HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr));
     table->table = (ListPtr *) malloc(sizeof(ListPtr) * tableSize);
     table->size = tableSize;

     for (i = 0; i < table->size; i++) {
         table->table[i] = createList();
     }

     return table;
 }


 void printTable(HashTablePtr table) {

     ListPtr tempList;
     NodePtr tempNode;
     HashObjectPtr obj;
     int i;

     for (i = 1; i < table->size; i++) {
         tempList = table->table[i];
         if (tempList->size != 0) {
             tempNode = tempList->head;
             obj = tempNode->HashObject;
             printf("%s\n\n", toString(obj));
         }
     }
 }

I think that the error has to due with using these lines:我认为错误必须归因于使用这些行:
tempList = table->table[i]; tempList = table->table[i];
table->table[i] = createList();表->表[i] = createList();
but I'm not sure how to fix it.但我不知道如何解决它。

Edit:编辑:

 typedef struct hashtable HashTable;
 typedef struct hashtable * HashTablePtr;

 struct hashtable {
     int size;
     ListPtr *table;
 };

Valgrind errors: Valgrind 错误:

999 errors in context 5 of 9:上下文 5 of 9 中的 999 个错误:
==73795== Invalid read of size 8 ==73795== 大小为 8 的读取无效
==73795== at 0x400B7D: printTable (HashTable.c:96) ==73795== 在 0x400B7D: printTable (HashTable.c:96)
==73795== by 0x400766: main (wf.c:16) ==73795== 0x400766:主要(wf.c:16)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== 地址 0x4c34048 是分配了大小为 8 的块之后的 0 个字节
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195) ==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17) ==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14) ==73795== 0x400753:主要(wf.c:14)
==73795== ==73795==
==73795== ==73795==
==73795== 1000 errors in context 6 of 9: ==73795== 9 个上下文中的 6 个错误:
==73795== Invalid read of size 8 ==73795== 大小为 8 的读取无效
==73795== at 0x400B2B: freeTable (HashTable.c:128) ==73795== 在 0x400B2B: freeTable (HashTable.c:128)
==73795== by 0x40076E: main (wf.c:17) ==73795== 由 0x40076E: 主要 (wf.c:17)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== 地址 0x4c34048 是分配了大小为 8 的块之后的 0 个字节
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195) ==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17) ==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14) ==73795== 0x400753:主要(wf.c:14)
==73795== ==73795==
==73795== ==73795==
==73795== 1000 errors in context 7 of 9: ==73795== 9 个上下文中的 7 个错误:
==73795== Invalid read of size 8 ==73795== 大小为 8 的读取无效
==73795== at 0x400D4C: createTable (HashTable.c:25) ==73795== 在 0x400D4C: createTable (HashTable.c:25)
==73795== by 0x400753: main (wf.c:14) ==73795== 0x400753:主要(wf.c:14)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd ==73795== 地址 0x4c34048 是分配了大小为 8 的块之后的 0 个字节
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195) ==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17) ==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14) ==73795== 0x400753:主要(wf.c:14)

 ListPtr createList() {
     ListPtr list;
     list = (ListPtr) malloc(sizeof(List));
     list->size = 0;
     list->head = NULL;
     list->tail = NULL;
     return list;
 }

The HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); is almost certainly wrong.几乎可以肯定是错误的。 You want to allocate enough storage for a HashTable, but it seems you're allocating storage for just a pointer to a HashTable(your HashTablePtr )您想为 HashTable 分配足够的存储空间,但似乎您只是为指向 HashTable 的指针分配存储空间(您的HashTablePtr

If you drop the habit of typedef'ing pointer and instead follow the approach of allocating in the following form, you won't get into that sort of problems:如果你放弃 typedef'ing 指针的习惯,而是按照以下形式分配的方法,你就不会遇到这种问题:

HashTable *table = malloc(sizeof *table);

Size of a pointer is always a fixed size and is architecture dependent.指针的大小始终是固定大小,并且取决于体系结构。 On 64-bit platforms, for example, it is always 8 bytes.例如,在 64 位平台上,它始终是 8 个字节。 Basically, sizeof (ObjectType) is not the same as sizeof (ObjectType *) .基本上, sizeof (ObjectType)sizeof (ObjectType *)不同。 So in this case you end up allocating less memory than you need which leads to segmentation faults.所以在这种情况下,你最终分配的 memory 比你需要的少,这会导致分段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM