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

2

My program, whose code is below, has the following error:

I do not understand why, I have plenty of memory in my PC, and all compiler sites have the same problem.

***Errorin'./prog':realloc():invalidpointer: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]=======Memorymap:========08048000-08049000r-xp0000000008:031295711/home/MhQXj9/prog08049000-0804a000rw-p0000000008:031295711/home/MhQXj9/prog0914d000-0916e000rw-p0000000000:000[heap]b763b000-b7656000r-xp0000000008:031303883/lib/i386-linux-gnu/libgcc_s.so.1b7656000-b7657000rw-p0001a00008:031303883/lib/i386-linux-gnu/libgcc_s.so.1b765b000-b765d000rw-p0000000000:000b765d000-b7806000r-xp0000000008:031303839/lib/i386-linux-gnu/i686/cmov/libc-2.17.sob7806000-b7807000---p001a900008:031303839/lib/i386-linux-gnu/i686/cmov/libc-2.17.sob7807000-b7809000r--p001a900008:031303839/lib/i386-linux-gnu/i686/cmov/libc-2.17.sob7809000-b780a000rw-p001ab00008:031303839/lib/i386-linux-gnu/i686/cmov/libc-2.17.sob780a000-b780d000rw-p0000000000:000b780d000-b784e000r-xp0000000008:031303836/lib/i386-linux-gnu/i686/cmov/libm-2.17.sob784e000-b784f000r--p0004000008:031303836/lib/i386-linux-gnu/i686/cmov/libm-2.17.sob784f000-b7850000rw-p0004100008:031303836/lib/i386-linux-gnu/i686/cmov/libm-2.17.sob7851000-b7856000rw-p0000000000:000b7856000-b7857000r-xp0000000000:000[vdso]b7857000-b7876000r-xp0000000008:031303796/lib/i386-linux-gnu/ld-2.17.sob7876000-b7877000r--p0001f00008:031303796/lib/i386-linux-gnu/ld-2.17.sob7877000-b7878000rw-p0002000008:031303796/lib/i386-linux-gnu/ld-2.17.sobf99a000-bf9af000rw-p0000000000:000[stack]

Theprogrambelowisahashtableanddoesthefollowing:

IhaveastructuredtypeTab[int*vetor,inttam_vetor],andconstantlyTab[n].vetorneedstobereallocatedtogrowinsize.

Attheendofeachnum_de_casoIreleasethe*vetoresandtheTabstructure.AndthenIcreatethemagain,untilIfinishthecycle.

#include<stdio.h>#include<stdlib.h>typedefstructtab{int*vetor;shortinttam_vetor;}Tab;voidinicializa(Tab*tabela,inttam){inti;for(i=0;i<tam;i++)tabela[i].tam_vetor=0;}inthash(intnum,inttam,inti)//geraumcodigopachave{num=(num%tam+i);returnnum;}voidinsere(Tab*tabela,intchave,intpos){tabela[pos].vetor[tabela[pos].tam_vetor]=chave;tabela[pos].tam_vetor=tabela[pos].tam_vetor+1;}voidliberaTabela(Tab*tabela,inttam_tabela){inti;for(i=0;i<tam_tabela;i++)if(tabela[i].vetor!=NULL)free(tabela[i].vetor);free(tabela);}voidverificaAlocacao(Tab*tabela,intpos){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;
}
    
asked by anonymous 30.10.2014 / 08:56

1 answer

1

Well ... what I learned in this problem was: Do not forget, never, in any way, initialize a vector of pointers 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;
    }
}
    
31.10.2014 / 03:33