简体   繁体   English

c++如何修复这段代码中的segmentation fault 11

[英]c++ how to fix the segmentation fault 11 in this code

I have a signal 11;我有一个信号 11; ie segfault .段错误 I know where it occurs (see code), but do not know how to correct it.我知道它发生在哪里(见代码),但不知道如何纠正它。

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 is an array of integers, judging from the following line: tempStats是一个整数数组,从以下行判断:

simulationStats.totalThreadNumProduced[i] = tempStats[0];

the segfault is either due to memory not being allocated for totalThreadNumProduced , or tempStats .段错误是由于没有为totalThreadNumProducedtempStats分配内存。

Furthermore, your usage for pthread_join is likely incorrect;此外,您对pthread_join的使用可能不正确; it simply returns whether the call was successful or not.它只是返回调用是否成功。 If you wanting the return value of a completed thread function, consider using the 2nd status parameter of pthread_join as show here .如果你想要完成的线程函数的返回值,可以考虑使用的第二个状态参数pthread_join作为展示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM