0
votes

So I'm implementing a heap using a resizable array and I keep getting this error every time it reallocates memory. The problem is with realloc ..I just can't figure out what's wrong with it. Here is the insert function:

void* insert (data_t *data, int room, long wake) {
 if(data->size+1 == data->arraySize){
    data->arraySize *= 2;
    long l = (long)data->arraySize;
    int* tempOne = realloc(data->heapElemOne, data->arraySize*sizeof(int));

    long* tempTwo = realloc(data->heapElemTwo, l*sizeof(long));

    if ( tempOne != NULL &&tempTwo !=NULL){ //realloc was
        data->heapElemOne = tempOne;
        data->heapElemTwo = tempTwo;
    }
    else{ //there was an error
        printf("Error allocating memory!\n");
        free(data->heapElemOne);
        free(data->heapElemTwo);
        return;
    }

  }
  data->size++;
  int now = data->size;

  /*Adjust its position*/
  if(data->size >0){

    while(data->heapElemTwo[now/2] > wake && ((now/2)!=0))
    {
            data->heapElemTwo[now] = data->heapElemTwo[now/2];
            data->heapElemOne[now] = data->heapElemOne[now/2];
            now /= 2;
    }
  }

  data->heapElemTwo[now] = wake;
  data->heapElemOne[now] = room;`

And Here is part of the main:

int main(int argc, char *argv[]){
    pthread_t r, c;
    data_t data;
    data.arraySize = 2;
    data.size = 0;
    long l = (long)data.arraySize;
    data.heapElemOne = malloc(data.arraySize * sizeof(int));
    data.heapElemTwo = malloc(l * sizeof(long));

Here's data_t declaration:

typedef struct{
    int arraySize;
    int* heapElemOne;
    long* heapElemTwo;
    int size;
    int number;
    pthread_mutex_t mutex;
    pthread_cond_t more;
}data_t;

It relocated the memory to 4 but when it's changing it to 8 it gives an error. Been at it for ages and just can't figure it out -_- Thanks in advance!

3
Can't figure out what you need actually. - Gopi
Could it be that in every call to malloc() and realloc() you want to multiply the number of elements to be allocated by sizeof( int ) or sizeof( long ) depending on the type of data you store? - ua2b
Post post the declaration of data_t. It sounds like you have a pointer & int reference problem. - David C. Rankin
updated with data_t declaration :) - scl

3 Answers

0
votes
malloc(data.arraySize)

This and all other calls to malloc and realloc are wrong. You want

malloc(data.arraySize * sizeof(int)) 

or, respectively,

malloc(data.arraySize * sizeof(long)) 

according to your element type. malloc and friends accept allocation size in bytes, and ints and longs are normally larger than one byte. So your arrays are allocated too short, and you are getting buffer overruns.

0
votes

the problem is almost certainly not with realloc, realloc has been tested in a wid range of circumstanses and has proved reliable. check that your code dos not exceed the bounds of the array of of any other allocated structures..

you mught be exceeding the upper bound on your allocation. realloc allocates chars but, your array is ints which are almost certainly larger than chars

try these changes: . int* tempOne = realloc(data->heapElemOne * sieof(int), data->arraySize); long* tempTwo = realloc(data->heapElemTwo, sizeof(long));

0
votes

It's cool I figured it out. I don't know how but for some reason when I added a cleanup function for my threads for when sigint is received the error went away :/ I'm a newbie to c..only been at it for a few days so I haven't a clue how cleanup fixed it :/ Thanks for the help though :) The way I was using malloc and realloc initially was also wrong so thanks for that too :)