pthread_create导致分段错误 [英] pthread_create causing segmentation fault
本文介绍了pthread_create导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pthread_t PThreadTable [32];
for(i = 1; i {
long i,Error;
printf(pthread_create%d!\\\
,i);
Error = pthread_create(& PThreadTable [i],NULL,
(void *(*)(void *))(SlaveStart),NULL);
if(Error!= 0)
{
printf(pthread_create()。\\\
中的错误);
exit(-1);
}
}
SlaveStart();
这段代码给调用 pthread_create
(通过 gdb
和 valgrind
检查)。为什么这样?
解决方案
这是因为您在循环内重新声明变量 i
。循环内的变量正在使用,它包含垃圾值。这就是为什么表达式& PThreadTable [i]
指向一个错误的地址,你会得到分段错误。
My program contains the following code.
pthread_t PThreadTable[32];
for (i=1; i<P; i++) // Checked with P = 4
{
long i, Error;
printf( "pthread_create %d!\n", i );
Error = pthread_create(&PThreadTable[i], NULL,
(void * (*)(void *))(SlaveStart), NULL);
if (Error != 0)
{
printf("Error in pthread_create().\n");
exit(-1);
}
}
SlaveStart();
The code gives segmentation fault on calling pthread_create
(checked through gdb
and valgrind
). Why so?
解决方案
Its because you redeclare variable i
inside the loop. The variable inside the loop is being used and it contains garbage value. That is why, the expression &PThreadTable[i]
points to a wrong address and you get a segmentation fault.
这篇关于pthread_create导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文