0

it seems that after the second time in which the function realocVet runs, the error message "malloc: *** error for object 0x7f8bfac039b0: pointer being realloc'd was not allocated" appears.

void realocVet (float *precoList, char *nomeList, short int *quantidadeList)
{
    static short int p=2,n=100,q=2;
    p=4*p;
    n=4*n;
    q=4*q;
    precoList =realloc(precoList,p * sizeof(float));
    nomeList =realloc(nomeList,n * sizeof(char));
    quantidadeList =realloc(quantidadeList,q * sizeof(short int ));
}

void insertData (float *precoList, char *nomeList, short int *quantidadeList, struct newCard myCard)
{
    static short int slotsAvailable = 2, aux=2,currentCard=0,nnchar=0;
    short int nchar;
    precoList[currentCard] = myCard.preco;
    quantidadeList[currentCard] = myCard.quantidade;
    for (nchar=0;nchar<50;nchar++)
    {
        nomeList[nnchar] = myCard.nome[nchar];
        nnchar++;
    }
    currentCard++;
    slotsAvailable--;

    if (slotsAvailable==0)
    {
        realocVet(precoList, nomeList, quantidadeList);
        slotsAvailable = aux;
        aux = 2*aux;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

4

You pass in a pointer, float *precoList, which you then realloc (potentially change). Problem is that when you return, the caller still has the old value of the pointer.

When you call the function again, it will be called with the old value that points to free'd memory.

void realocVet (float **precoList, char **nomeList, short int **quantidadeList)
{
    static short int p=2,n=100,q=2;
    p=4*p;
    n=4*n;
    q=4*q;
    *precoList =realloc(*precoList,p * sizeof(float));
    *nomeList =realloc(*nomeList,n * sizeof(char));
    *quantidadeList =realloc(*quantidadeList,q * sizeof(short int ));
}
K Scott Piel
  • 4,320
  • 14
  • 19
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Oh, I get it... It makes sense. How about the call of the function realocVet then? Should I make some change? ps: still not working, but I'm hopeful it will :p –  May 02 '13 at 19:43