c - 重新分配错误: realloc(): invalid next size

标签 c hashtable realloc

我找不到以下问题的解决方案。这个问题我找了好多次了,还是不知道怎么解决。

我必须做什么: 我必须编写一个程序来读取包含随机推文的存档并将其保存在矩阵中。之后,用户应该能够编写单词列表。该程序必须读取每个单词并向用户显示包含该单词的推文。

我的解决方案: 程序读取矩阵中的存档后,推文中的每个单词都会进入哈希函数。散列函数告诉矩阵中推文的索引应进入散列表的何处。哈希表的工作原理类似于整数矩阵。哈希表的每个索引都有一个指向数组的指针,该数组包含推文所在矩阵的索引。

问题: realloc 函数工作得不太好。进行一些插入后,该函数会停止程序并显示错误:* Error in `./a.out': realloc(): invalid next size: 0x00000000023f2460 *

我认为这是因为该函数试图访问哈希表的无效位置,但我不确定。

存档中的推文如下所示:“14,0,jb 不再在澳大利亚显示!”。每行包含 3 个信息,以逗号分隔。

我的“int main()” -> 读取存档并调用将矩阵索引插入哈希表的函数:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAT_SIZE 10000
#define TABLE_SIZE 10000

int main(){
FILE *fp;
char str[300];
char matriz[MAT_SIZE][300];
char *token;
int **TabelaHash;
int i, j, pos, verifica;
pos = i = j = 0;

TabelaHash = criaHash();
fp = fopen("corpus.csv","r");

if(fp == NULL)
{
    printf("Erro ao abrir o arquivo!");
    exit(1);
}
while(fgets(str, 300, fp) != NULL)
{
    token = strtok(str, ",");
    token = strtok(NULL, ",");
    token = strtok(NULL, ",");
    removeEspacosIniciais(matriz, token, pos); // Remove the initial spaces of the string and saves in the matrix
    token = strtok(matriz[pos], " ");
    while(token != NULL){
        verifica = insertHash(TabelaHash, token, pos);
        if(verifica != 1){
            printf("Ocorreu um Erro!\n");
            exit(1);    
        }
        token = strtok(NULL, " ");
    }
    pos++;

}

freeHash(TabelaHash);

return 0;
}

创建哈希表的函数:

int** criaHash(){
int **ha, i;
ha = (int**) malloc(TABLE_SIZE * sizeof(int*));
if(ha != NULL){
    for(i = 0; i < TABLE_SIZE; i++){
        ha[i] = (int*) malloc(sizeof(int));
        ha[i][0] = 0; // The position ha[i][0] is a counter which indicates how many indexes are going to be realocated in the memory
    }

    return ha;
}
}

插入哈希表的函数:

int insertHash(int **ha, char *word, int index){
    if(ha == NULL)
        return 0;

    int key = stringValue(word); // stringValue is the hash function, returns an integer which is the index of the hash table
    int cont = 1;   

    int *temp = (int*) realloc(ha[key], sizeof(int));
    if(temp == NULL)
        return 0;
    else
        ha[key] = temp;

    ha[key][0]++; // ha[i][0] counts the size of the line "i" in the hash table
    cont = ha[key][0];
    ha[key][cont] = indice;   // Inserts the indice of the matrix into the hash table

    return 1;
}

抱歉我的英语想法,希望你能帮助我。 谢谢大家!

最佳答案

关于这一点:

问题:realloc 函数工作得不太好。一些插入后,该函数停止程序并显示错误: * Error in `./a.out': realloc(): invalid next size: 0x00000000023f2460 *

对任何内存分配函数(malloc、calloc、realloc)的调用始终会在堆中查找足够大且包含所请求字节数的内存块。为此,它会查看这些分配的内存块之间的链接。当这些链接之一不正确(NULL 或超出堆的范围等)时,它将返回错误。

代码产生错误,因为每次写入哈希表(0 索引除外)都会覆盖这些链接

关于c - 重新分配错误: realloc(): invalid next size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911973/

相关文章:

c++ - 哈希表需要大量内存

performance - 哈希表中的链接

c - 意外的 realloc() 行为

c - 为什么它返回我段错误(核心转储)?

java - 我应该如何为 JNI 加载 native 库以避免 UnsatisfiedLinkError?

c - 如何从内联汇编访问 C 结构/变量?

c - 卡在 c 程序中如何在进程中间停止 scanf

调用 Fortran 例程的 C MPI 程序崩溃

arrays - 为什么 Lucene 使用数组而不是哈希表作为倒排索引?

c - 使用动态数组进行 realloc() 后的意外行为