mardi 21 juin 2016

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. Well, let's see the code then.

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.

Reads the archive and calls the function that inserts the index of the matrix into the hash table:

while(fgets(str, 300, fp) != NULL)
{
    token = strtok(matriz[pos], " ");
    while(token != NULL){
        flag = insertHash(HashTable, token, pos);
        if(flag != 1){
            printf("ERROR!n");
            exit(1);    
        }
        token = strtok(NULL, " ");
    }
    pos++;

}

Function that inserts into the hash table:

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

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

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

    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!

Aucun commentaire:

Enregistrer un commentaire