What does the error mean: realloc(): invalid Pointer: 0xb78094d8?

Asked

Viewed 453 times

2

My program, whose code is further down, has shown the following error:

I don’t understand because I have too much memory on my PC, and all the compiler sites have the same problem.

erro

*** Error in `./prog': realloc(): invalid pointer: 0xb78094d8 ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x75e72)[0xb76d2e72]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(realloc+0x275)[0xb76d6ad5]
./prog[0x804866c]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xf5)[0xb76768f5]
./prog[0x80486ad]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 1295711    /home/MhQXj9/prog
08049000-0804a000 rw-p 00000000 08:03 1295711    /home/MhQXj9/prog
0914d000-0916e000 rw-p 00000000 00:00 0          [heap]
b763b000-b7656000 r-xp 00000000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b7656000-b7657000 rw-p 0001a000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b765b000-b765d000 rw-p 00000000 00:00 0 
b765d000-b7806000 r-xp 00000000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7806000-b7807000 ---p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7807000-b7809000 r--p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7809000-b780a000 rw-p 001ab000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b780a000-b780d000 rw-p 00000000 00:00 0 
b780d000-b784e000 r-xp 00000000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b784e000-b784f000 r--p 00040000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b784f000-b7850000 rw-p 00041000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b7851000-b7856000 rw-p 00000000 00:00 0 
b7856000-b7857000 r-xp 00000000 00:00 0          [vdso]
b7857000-b7876000 r-xp 00000000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b7876000-b7877000 r--p 0001f000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b7877000-b7878000 rw-p 00020000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
bf99a000-bf9af000 rw-p 00000000 00:00 0          [stack]

the program below is a hash table and does the following:

I have a structured type Tab[int* vetor, int tam_vetor], and constantly the Tab[n].vetor needs to be reallocated to grow in size.

At the end of each num_de_caso I release the *vetores and the structure Tab. And then I create them again, until the cycle is over.

#include <stdio.h>
#include <stdlib.h>

typedef struct tab
{
    int* vetor;
    short int tam_vetor;

}Tab;

void inicializa(Tab* tabela, int tam)
{
    int i;

    for(i=0; i < tam; i++)
        tabela[i].tam_vetor = 0;
}

int hash(int num,int tam, int i)//gera um codigo p a chave
{
    num = (num%tam + i);

    return num;
}

void insere(Tab* tabela, int chave, int pos)
{
    tabela[pos].vetor[tabela[pos].tam_vetor] = chave;
    tabela[pos].tam_vetor = tabela[pos].tam_vetor + 1;
}

void liberaTabela(Tab* tabela, int tam_tabela)
{
    int i;

    for(i=0; i<tam_tabela;i++)
        if(tabela[i].vetor != NULL)
            free(tabela[i].vetor);

    free(tabela);

}

void verificaAlocacao(Tab* tabela, int pos)
{
    if(pos < 0)
    {
        if(tabela == NULL)
        {
            printf("\nmemoria insuficiente");
            exit(1);
        }
    }
    else
    {
        if(tabela[pos].vetor == NULL)
        {
            printf("\nmemoria insuficiente");
            exit(1);
        }
    }
}

void printTabela(Tab* tabela, int tam_tabela)
{
    int i,j;

    for(i=0; i< tam_tabela; i++)
    {
        printf("%d ->",i);

        if(tabela[i].vetor != NULL)
        {
            for(j=0; j < tabela[i].tam_vetor; j++)
            {
                printf(" %d ->",tabela[i].vetor[j]);
            }
        }

        printf(" \\\n");
    }
}

int main()
{
    int num_de_casos,
        tam_tabela,
        num_chaves,
        chave,
        pos,
        i,
        j;

    Tab* tabela;

    scanf("%d", &num_de_casos);

    while(num_de_casos != 0)
    {
        scanf("%d",&tam_tabela);
        scanf("%d",&num_chaves);
        tabela = (Tab*) malloc(tam_tabela*sizeof(Tab));
        inicializa(tabela, tam_tabela);
        verificaAlocacao(tabela, -1);

        while(num_chaves != 0)
        {
            scanf("%d",&chave);

            i=0;

            pos = hash(chave,tam_tabela,i);

            while(1)
            {
                if(tabela[pos].vetor == NULL)
                {
                    tabela[pos].vetor = (int*) malloc(sizeof(int));
                    verificaAlocacao(tabela, pos);
                    insere(tabela,chave,pos);
                    break;
                }
                else
                {
                    for(j=0; j < tabela[pos].tam_vetor && chave != tabela[pos].vetor[j]; j++);//acha j<tam_vetor se: chave == algum elemento && acha j == tam_vetor se: chave não coincide com nenhum elemento

                    if(j == tabela[pos].tam_vetor)
                    {
                        tabela[pos].vetor = (int*) realloc(tabela[pos].vetor,(j+2)*sizeof(int));
                        verificaAlocacao(tabela, pos);
                        insere(tabela,chave,pos);
                        break;
                    }
                    else
                    {
                       i++;
                       pos = hash(chave,tam_tabela,i);
                    }
                }
            }

            num_chaves--;
        }

        printTabela(tabela, tam_tabela);
        liberaTabela(tabela, tam_tabela);
        num_de_casos--;
    }

    return 0;
}
  • Try using http://valgrind.org/. It may give you a more precise indication of your current problem.

  • 1

    The problem happens in its liberated functionTable it is accessing a vector that apparently has not been filled. Also note the output of the printTable() function, you will notice that the last print comes out empty. Now we know where the problem is

  • @Zuul like that to use Valgrind. I thank you!

  • @Raulsenaferreira I don’t understand the pq of being the liberated functionTable()... because it is never empty. All input data goes to it. A the function printTable() has to have that ending, to be able to leave this way in print: \

  • 1

    If you comment on the function freeTable() your code runs partially, so the chances of your problem being there is great. Now it is necessary that you take a calm look at this function and debug in search of the error and post here the doubts and advances.

1 answer

1


Well... what I learned in this problem was: Never forget to initialize a pointer vector with NULL;

void inicializa(Tab* tabela, int tam)
{
    int i;

    for(i=0; i < tam; i++)
    {   
        tabela[i].vetor = NULL;//a falta dessa linha ocasionou o erro de memóriaN

        tabela[i].tam_vetor = 0;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.