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);
}
(gdb) p rootName[0]
Cannot access memory at address 0xfffffffff001e5c0
unsigned short int* getPointer2(unsigned short int* buf)
{
unsigned short int* temp = buf;
temp[0] = 67;
return temp;
}
void mallocTest()
{
unsigned short int buf[100];
unsigned short int* temp = getPointer2(buf);//getPointer();
int i=0;
for(;temp[i]!=0;i++)
{
printf("%c", temp[i]);
}
//free(temp);
}
如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
[解决办法]
明显就是未声明函数,默认返回int64导致的