我寫了一個程序來創建10個線程並正常運行。該程序運行良好,但最終會出現分段錯誤。這是什麼故障,是什麼原因造成的,我該如何解決? 我的代碼是:程序執行期間的分段錯誤
#include<stdio.h>
#include<pthread.h>
void *print(void *num);
int main()
{
pthread_t tid[10];
int n,check;
void *exitstatus;
for(n=1;n<=10;n++)
{
check=pthread_create(&tid[n],NULL,print,(void *)&n);
if(check=0)
printf("thread created");
pthread_join(tid[n],&exitstatus);
}
return 0;
}
void *print(void *num)
{
int i,*val=(int *)num;
for(i=0;i<(5);i++)
printf("Hello World!!!(thread %d)\n",*val);
}
您是否嘗試過使用gdb來隔離錯誤的來源? – ziu 2013-03-21 12:41:57