程序执行过程中的分段错误

use*_*010 1 c linux gcc pthreads

我编写了一个程序来创建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);
}
Run Code Online (Sandbox Code Playgroud)

mas*_*oud 7

你有很多瑕疵:

for(n=1;n<=10;n++) // No, The array starts from 0 and lasts on 9
Run Code Online (Sandbox Code Playgroud)

试试这个

for(n=0;n<10;n++)
Run Code Online (Sandbox Code Playgroud)
if(check=0) // No, this will assign 0 to check instead of compare it
Run Code Online (Sandbox Code Playgroud)

试试这个

if(check==0)
Run Code Online (Sandbox Code Playgroud)