C 带有返回值的pthread_join给出了分段错误

C 带有返回值的pthread_join给出了分段错误,c,pthreads,posix,pthread-join,C,Pthreads,Posix,Pthread Join,我试图运行下面的代码,该代码使用pthread_create创建一个线程,并从线程内部返回计数。代码给了我一个分段错误 #include <pthread.h> #include <stdlib.h> #include <string.h> #include <stdio.h> void *test_routine(void *arg); int main(int argc, char **argv) { pthread_t first

我试图运行下面的代码,该代码使用pthread_create创建一个线程,并从线程内部返回计数。代码给了我一个分段错误

#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void *test_routine(void *arg);

int main(int argc, char **argv)
{
    pthread_t first_thread; 
    void *thread_return;
    int result = 0;
    
    result = pthread_create(&first_thread, NULL, test_routine, NULL);
    if (0 != result) 
    {
        fprintf(stderr, "Failed to create thread %s\n", strerror(result));
        exit(1);
    }
    
    result = pthread_join(first_thread, &thread_return);
    
    if (0 != result) {
        fprintf(stderr, "Failed to join a thread: %s\n", strerror(result));
        pthread_exit(NULL);
    }
    printf("\nValue returning from the test routine  %d\n", (int) thread_return);

   free(thread_return);
   exit(3);
}

void *test_routine(void *arg) 
{
    int count = 0;
    count++;
    pthread_exit(count);
}
#包括
#包括
#包括
#包括
void*测试程序(void*arg);
int main(int argc,字符**argv)
{
pthread\u t第一个线程;
无效*线程返回;
int结果=0;
结果=pthread_create(&first_thread,NULL,test_例程,NULL);
如果(0!=结果)
{
fprintf(stderr,“未能创建线程%s\n”,strerror(result));
出口(1);
}
结果=pthread\u join(第一个线程,&线程返回);
如果(0!=结果){
fprintf(stderr,“未能加入线程:%s\n”,strerror(result));
pthread_exit(NULL);
}
printf(“\n从测试例程%d\n返回的值,(int)线程返回);
自由(线程返回);
出口(3);
}
void*test_例程(void*arg)
{
整数计数=0;
计数++;
pthread_退出(计数);
}

线程不包含从
malloc
返回的指针时,您正在将
线程返回
传递到
空闲
。它包含一个已转换为指针的整数值

您应该只将从
malloc
返回的内容传递到
free
,因此删除对
free
的调用