[英]Realloc Error: realloc(): invalid next size
我找不到以下问题的解决方案。 我已经搜索了太多次了,但是我仍然不知道如何解决。
我必须做的:我必须编写一个程序,该程序使用随机推文读取存档,并将其保存在矩阵中。 之后,应该使用户能够编写单词列表。 该程序必须阅读每个单词,并向用户显示其中包含单词的那些推文。
我的解决方案:程序读取矩阵形式的档案后,推文中的每个单词都将传递给哈希函数。 哈希函数告诉矩阵中tweet的索引应移至哈希表的位置。 哈希表的工作方式类似于整数矩阵。 哈希表的每个索引都有一个指向数组的指针,该数组具有推文所在的矩阵的索引。
问题: realloc函数不能很好地工作。 插入后,该函数将停止程序并显示错误: *`./a.out'错误:realloc():下一个无效大小: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函数不能很好地工作。 插入后,该函数将停止程序并显示错误:*`./a.out'错误:realloc():下一个无效大小:0x00000000023f2460 *
对任何内存分配函数(malloc,calloc,realloc)的调用始终在堆中寻找一块足以容纳所请求字节数的内存块。 为此,它将查看那些分配的内存块之间的链接。 当这些链接之一不正确时(NULL或超出堆的范围,等等),则返回错误。
该代码产生错误,因为每次对哈希表(0索引除外)的写操作都会覆盖这些链接
当询问有关运行时问题的问题时:
发布的代码不完整,无法完全编译。
注意:当发布的代码甚至无法编译时,我们不太可能为您解决运行时问题。
implicit declaration of function 'criahash()'
assignment makes pointer from integer without a cast
Tabelahash = criaHash();
implicit declaration of function: 'removeEspacoslniciais()'
implicit declaration of function: 'InsertHash()'
implicit declaration of function: 'freeHash()'
conflicting types for 'criaHash()'
implicit declaration of function 'stringValue()'
'indice' undeclared
unused parameter 'index'
control reaches end of non-void function: 'criahash()'
编译时,请始终启用所有警告,然后修复这些警告
适当的原型语句将修复其中一些警告,但不是全部,也不会修复任何错误。
对于gcc
,可以编译使用:
gcc -Wall -Wextra -pedantic -Wconversion -std=gnu99 -c -ggdb fileName.c -o fileName.o
对于gcc
,链接使用:
gcc -ggdb fileName.o -o fileName
注意:对于(希望)与问题不相关的函数,只需发布原型语句
请解决问题,然后发布包含更正的其他文字
很抱歉没有发布完整的代码和问题。 我不知道该怎么问。 好吧,代码现在正在工作...
该问题由user3629249解释。 我使用了具有定义大小的malloc来固定它到哈希表中的所有指针。
感谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.