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

发布于 2024-11-03 22:19:40 字数 2889 浏览 1 评论 0原文

我正在编写一个程序,它使用链接列表的哈希表来计算单词的频率。该程序将计算我输入的所有单词以及频率,但是在打印哈希表后,我收到分段错误(核心转储)错误。当我对我的程序进行 valgrind 时,它显示我在三个不同的地方遇到错误,这些错误是大小 8 的无效读取。但我不知道如何修复它们。这是三个不同的地方:

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

我认为错误是由于使用这些行造成的:
tempList = 表->表[i];
表->表[i] = createList();
但我不知道如何解决它。

编辑:

 typedef struct hashtable HashTable;
 typedef struct hashtable * HashTablePtr;

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

Valgrind 错误:

上下文 5 中的 999 个错误(共 9 个):
==73795== 大小 8 的读取无效
==73795== 在 0x400B7D: printTable (HashTable.c:96)
==73795== 0x400766:主要(wf.c:16)
==73795== 地址 0x4c34048 是分配大小为 8 的块之后的 0 个字节
==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== 由 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 上下文 6(共 9 个)中有 1000 个错误:
==73795== 大小 8 的读取无效
==73795== 在 0x400B2B: freeTable (HashTable.c:128)
==73795== by 0x40076E: main (wf.c:17)
==73795== 地址 0x4c34048 是分配大小为 8 的块之后的 0 个字节
==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== 由 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 上下文第 7 个错误(共 9 个)中有 1000 个错误:
==73795== 大小 8 的读取无效
==73795== 在 0x400D4C: createTable (HashTable.c:25)
==73795== by 0x400753: main (wf.c:14)
==73795== 地址 0x4c34048 是分配大小为 8 的块之后的 0 个字节
==73795== 在 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== 由 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)

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

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. 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];

table->table[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:

999 errors in context 5 of 9:

==73795== Invalid read of size 8

==73795== at 0x400B7D: printTable (HashTable.c:96)

==73795== by 0x400766: main (wf.c:16)

==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 1000 errors in context 6 of 9:
==73795== Invalid read of size 8
==73795== at 0x400B2B: freeTable (HashTable.c:128)
==73795== by 0x40076E: main (wf.c:17)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)
==73795==
==73795==
==73795== 1000 errors in context 7 of 9:
==73795== Invalid read of size 8
==73795== at 0x400D4C: createTable (HashTable.c:25)
==73795== by 0x400753: main (wf.c:14)
==73795== Address 0x4c34048 is 0 bytes after a block of size 8 alloc'd
==73795== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
==73795== by 0x400D05: createTable (HashTable.c:17)
==73795== by 0x400753: main (wf.c:14)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝咒 2024-11-10 22:19:40

HashTablePtr table = (HashTablePtr) malloc(sizeof(HashTablePtr)); 几乎肯定是错误的。您想为 HashTable 分配足够的存储空间,但似乎您只是为指向 HashTable 的指针(您的 HashTablePtr)分配存储空间,

如果您放弃 typedef'ing 指针的习惯,而是遵循采用以下形式分配的方法,您不会遇到此类问题:

HashTable *table = malloc(sizeof *table);

The 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)

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:

HashTable *table = malloc(sizeof *table);
九公里浅绿 2024-11-10 22:19:40

指针的大小始终是固定大小并且取决于体系结构。例如,在 64 位平台上,它始终为 8 字节。基本上,sizeof (ObjectType)sizeof (ObjectType *) 不同。因此,在这种情况下,您最终分配的内存比您需要的要少,这会导致分段错误。

Size of a pointer is always a fixed size and is architecture dependent. On 64-bit platforms, for example, it is always 8 bytes. Basically, sizeof (ObjectType) is not the same as sizeof (ObjectType *). So in this case you end up allocating less memory than you need which leads to segmentation faults.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文