c语言malloc崩溃,malloc 跟 thread 同时, 崩溃

malloc 和 thread 同时, 崩溃unsignedshortint*getPointer(){unsignedshortint*buf=(unsignedshortint*)malloc(4*sizeof(unsignedshortint));unsignedshortint*temp=buf;buf[0]=67;returntemp;}voidma...
摘要由CSDN通过智能技术生成

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调

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值