malloc 和 thread 同时, 崩溃
unsigned short int* getPointer()
{
unsigned short int* buf = (unsigned short int*)malloc(4*sizeof(unsigned short int));
unsigned short int* temp = buf;
buf[0]=67;
return temp;
}
void mallocTest()
{
unsigned short int* temp = getPointer();
int i=0;
for(;temp[i]!=0;i++)
{
printf("%c", temp[i]);
}
free(temp);
}
void mallocTreadTest()
{
pthread_t a_thread;
pthread_create(&a_thread, NULL, (void*)mallocTest, NULL);
void* threadresult;
pthread_join(a_thread, &threadresult);
}
为了说明问,代码作了简化.getPointer里调用了malloc,申请了内存.
不用多线程的时候,代码没有问题.
当前代码放在同一个.c文件里运行,也没问题.
奇怪就奇怪在getPointer函数写在另一个.c文件里(对应的.h文件什么的都写好),mallocTest调