簡體   English   中英

c++如何修復這段代碼中的segmentation fault 11

[英]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];

段錯誤是由於沒有為totalThreadNumProducedtempStats分配內存。

此外,您對pthread_join的使用可能不正確; 它只是返回調用是否成功。 如果你想要完成的線程函數的返回值,可以考慮使用的第二個狀態參數pthread_join作為展示在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM