1

I keep getting this "glibc detected free(): invalid next size (fast)" error but dont know exactly why. I read it is because of an out of bounds error but dont see anywhere in my code where this could be happening, does anyone see anything I missed?

Here is my code:

typedef struct
{
int* inputData;
int* histogramData;
int numElements;
pthread_t* tid;
} threadInput;


void* threadRoutine(void* argv)
{

// initializers
int i, avgInputSize, lastInputSize, threadStart, threadEnd, threadNum, numThreadsUsed;

// stores input data into tempData
threadInput* tempData = (threadInput*) argv;

// calculates the required number of threads
numThreadsUsed = NUM_THREADS; 
if(NUM_THREADS > tempData->numElements) 
{
    numThreadsUsed = tempData->numElements;
}


// create histogram
for(threadNum = 0; threadNum < numThreadsUsed; threadNum++)
{
    if(tempData->tid[i] == pthread_self())
    {

        // finds start and end of data set for thread
        if(tempData->numElements > numThreadsUsed)
        {
            avgInputSize = (int)((tempData->numElements)/NUM_THREADS);
            threadStart = threadNum*avgInputSize;
            if(i < (NUM_THREADS-1)) 
            {
                threadEnd = ((threadNum+1)*avgInputSize);
            }
            else if(i == (NUM_THREADS-1)) 
            {
                threadEnd = (tempData->numElements);        
            }
        }
        else
        {
            threadStart = i;
            threadEnd = i + 1;
        }


        // creates histogram
        pthread_mutex_lock(&lock);

        for(i = threadStart; i < threadEnd; i++)
        {
            tempData->histogramData[tempData->inputData[i]]++;
        }

        pthread_mutex_unlock(&lock);
    }
}


pthread_exit(0);
}


void compute_using_pthreads(int *input_data, int *histogram, int num_elements, int histogram_size)
    {
    // initializers
    int i, j;
    threadInput* input = malloc(sizeof(threadInput*));
    input->inputData = malloc(sizeof(input_data));
    input->histogramData = malloc(sizeof(histogram));
input->tid = malloc(NUM_THREADS*sizeof(pthread_t));

// enters data into struct
    input->inputData = input_data;
input->histogramData = histogram;

// Create threads
    for(i = 0; i <  NUM_THREADS; i++)
            pthread_create(&input->tid[i], NULL, threadRoutine, (void*) &input);

// reaps threads
    for(i = 0; i <  NUM_THREADS; i++)
            pthread_join(input->tid[i], NULL);

    pthread_mutex_destroy(&lock);

    // frees space
    free(input->inputData);
    free(input->histogramData);
    free(input->tid);
    free(input);


}
halex
  • 16,253
  • 5
  • 58
  • 67
kdhalljr
  • 21
  • 1

1 Answers1

2

This is a mistake:

threadInput* input = malloc(sizeof(threadInput*));

as it is only allocating enough space for threadInput*, when it should be allocating for a threadInput:

threadInput* input = malloc(sizeof(*input));

Similar error with input_data and histogram in that a sizeof(int*) is being allocated instead of sizeof(int).

hmjd
  • 120,187
  • 20
  • 207
  • 252