我不断收到此“glibc 检测到 free(): invalid next size (fast)”错误,但不知道具体原因。我读到它是因为越界错误,但在我的代码中没有看到可能发生这种情况的任何地方,有没有人看到我错过的任何东西?
这是我的代码:
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);
}