2012-10-21 73 views
1

我不斷收到這個「glibc檢測到免費():無效的下一個大小(快)」的錯誤,但不知道到底爲什麼。我讀到它是因爲出界的錯誤,但沒有看到我的代碼中可能發生的任何地方,有沒有人看到我錯過了什麼?glibc檢測到免費(),同時使用posix線程C

這裏是我的代碼:

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); 


} 
+1

當你在調試器下運行它時,它會發生什麼錯誤? –

+0

'threadInput.numElements'永遠不會被填充。 – saeedn

回答

2

這是一個錯誤:

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

,因爲它只是爲threadInput*分配足夠的空間,當它應該被分配用於threadInput

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

input_data和類似的錯誤,因爲分配了sizeof(int*)而不是sizeof(int)

相關問題