[英]c++ how to fix the segmentation fault 11 in this code
我有一個信號 11; 即段錯誤。 我知道它發生在哪里(見代碼),但不知道如何糾正它。
int *totalThreadNumProduced; //array
int *totalThreadNumConsumed; //array
int *tempStats; //array
for(int i=0; i < global_args.numProducers; i++)
{
tempStats = (int*) pthread_join( tidP[i], NULL );
simulationStats.totalThreadNumProduced[i] = tempStats[0]; //where the segmentation fault 11 is
simulationStats.numTimesBufferFull += tempStats[1];
}
tempStats = (int*) pthread_join( tidP[i], NULL );
tempStats
是一個整數數組,從以下行判斷:
simulationStats.totalThreadNumProduced[i] = tempStats[0];
段錯誤是由於沒有為totalThreadNumProduced
或tempStats
分配內存。
此外,您對pthread_join
的使用可能不正確; 它只是返回調用是否成功。 如果你想要完成的線程函數的返回值,可以考慮使用的第二個狀態參數pthread_join
作為展示在這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.