Matheus Moraes
Matheus Moraes

Reputation: 3

Realloc Error: realloc(): invalid next size

I coundn't find a solution for the problem below. I have searched for this too many times, but I still don't know how to solve.

What I have to do: I have to make a program that reads an archive with random tweets and saves it in a matrix. After that, the user should be enable to write a list of words. The program have to read each word and show to the user those tweets that have the word in it.

My solution: After the program reads the archive in a matrix, each word in the tweets goes to a hashing function. The hashing function tells where the index of the tweet in the matrix should go to an hash table. The hash table works like a matrix of integers. Each index of the hash table have a pointer to an array with the indexes of the matrix where the tweets are.

The problem: The realloc function isn't working very well. After some insertions, the function stops the program and shows an Error: * Error in `./a.out': realloc(): invalid next size: 0x00000000023f2460 *

I think it's because the function is trying to acces an invalid position of the hash table, but I don't know for sure.

The tweets in the archive looks like this: "14,0, jb isnt showing in australia any more!". Each line contains 3 informations separated by a comma.

My "int main()" -> Reads the archive and calls the function that inserts the index of the matrix into the hash table:

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

Function that creates the hash table:

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

Function that inserts into the hash table:

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

Sorry for my english thought and I hope you can help me. Thanks Everyone!

Upvotes: 0

Views: 3405

Answers (3)

Matheus Moraes
Matheus Moraes

Reputation: 3

Sorry for not posting the entire code and the problems. I didn't know how I should ask the question. Well, the code is working now...

The problem was explained by the user3629249. I fixed it using a malloc with a defined size to all the pointers in the hash table.

Thanks everyone!

Upvotes: 0

user3629249
user3629249

Reputation: 16540

When asking a question about a runtime problem:

  1. post code that cleanly compiles
  2. post code that is short, but still exhibits the problem

The posted code is not complete and does not cleanly compile.

Note: we are unlikely to be able to help you with a runtime problem, when the posted code does not even compile.

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()'

When compiling, always enable all the warnings, then fix those warnings

Appropriate prototype statements would fix some of those warnings, but not all of them and would not fix any of the errors.

for gcc, to compile use:

gcc -Wall -Wextra -pedantic -Wconversion -std=gnu99 -c -ggdb fileName.c  -o fileName.o

for gcc, to link use:

gcc -ggdb fileName.o -o fileName

Note: for functions that are (hopefully) not relevant to the problem, just post the prototype statement

Please fix the problems, then, post additional text with the corrections

Upvotes: 0

user3629249
user3629249

Reputation: 16540

regarding this:

The problem: The realloc function isn't working very well. After some insertions, the function stops the program and shows an Error: * Error in `./a.out': realloc(): invalid next size: 0x00000000023f2460 *

A call to any of the memory allocation functions (malloc, calloc, realloc) always is looking for a block of memory in the heap that is large enough contain the number of bytes requested. To do that, it looks at the links between those allocated memory blocks. When one of those links is not correct (NULL or out of the bounds of the heap, etc) then it returns the error.

The code is producing the error because each write to the hash table (other than a 0 index) is overwriting those links

Upvotes: 1

Related Questions