glibc在C中使用posix线程时检测到free()
我一直收到“glibc detected free():无效的下一个大小(fast)”错误,但不知道确切原因。我读到它是因为一个越界错误,但在我的代码中没有看到任何可能发生这种情况的地方,有人看到我遗漏了什么吗 这是我的密码:glibc在C中使用posix线程时检测到free(),c,multithreading,posix,free,glibc,C,Multithreading,Posix,Free,Glibc,我一直收到“glibc detected free():无效的下一个大小(fast)”错误,但不知道确切原因。我读到它是因为一个越界错误,但在我的代码中没有看到任何可能发生这种情况的地方,有人看到我遗漏了什么吗 这是我的密码: typedef struct { int* inputData; int* histogramData; int numElements; pthread_t* tid; } threadInput; void* threadRoutine(void* argv) {
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);
}
typedef结构
{
int*输入数据;
int*历史记录数据;
国际货币基金组织;
pthread_t*tid;
}线程输入;
void*线程例程(void*argv)
{
//初始化者
int i、avgInputSize、lastInputSize、threadStart、threadEnd、threadNum、numThreadsUsed;
//将输入数据存储到tempData中
threadInput*tempData=(threadInput*)argv;
//计算所需的线程数
numThreadsUsed=NUM_线程;
if(NUM_THREADS>tempData->numElements)
{
numThreadsUsed=tempData->numElements;
}
//创建直方图
for(threadNum=0;threadNumtid[i]==pthread_self())
{
//查找线程的数据集的开始和结束
if(tempData->numElements>numThreadsUsed)
{
avgInputSize=(int)((tempData->numElements)/NUM_线程);
threadStart=threadNum*avgInputSize;
如果(i<(NUM_-1))
{
threadEnd=((threadNum+1)*avgInputSize);
}
else if(i==(NUM_-1))
{
threadEnd=(tempData->numElements);
}
}
其他的
{
threadStart=i;
threadEnd=i+1;
}
//创建直方图
pthread_mutex_lock(&lock);
用于(i=threadStart;ihistorogramdata[tempData->inputData[i]]+;
}
pthread_mutex_unlock(&lock);
}
}
pthread_退出(0);
}
使用线程无效计算(int*输入数据、int*直方图、int num元素、int直方图大小)
{
//初始化者
int i,j;
threadInput*input=malloc(sizeof(threadInput*));
输入->输入数据=malloc(sizeof(输入数据));
输入->HistorogramData=malloc(直方图大小);
输入->tid=malloc(NUM_THREADS*sizeof(pthread_t));
//将数据输入结构
输入->输入数据=输入数据;
输入->直方图数据=直方图;
//创建线程
对于(i=0;itid[i],NULL,threadRoutine,(void*)&input);
//收获线程
对于(i=0;itid[i],NULL);
pthread_mutex_destroy(&lock);
//释放空间
自由(输入->输入数据);
免费(输入->历史记录数据);
免费(输入->tid);
免费(输入);
}
这是一个错误:
threadInput* input = malloc(sizeof(threadInput*));
因为它只为threadInput*
分配了足够的空间,所以当它应该为threadInput
分配空间时:
threadInput* input = malloc(sizeof(*input));
与
input_data
和threadInput
类似的错误是,分配的是sizeof(int*)
,而不是sizeof(int)
在调试器下运行时,它在哪一行出错?threadInput。numElements
从不填充。