紧急求助,多线程malloc和free的问题

紧急求助,多线程malloc和free的问题,第1张

test返回的是一个指针,如果你在display中调用了test,那你在display中肯定会要对test返回的值进行存储,例如int*p=test,那么test中申请的空间就保留了,要释放这部分内存只要对p *** 作就可以了,而i_test这个变量在test执行完就释放了,所以如果你没有类似int*p=test这样的语句,则申请的空间就会变成黑户,你无法找到它,它也不会释放

pthread_join 线程停止等待函数没有调用

pthread_create 线程生成后,没有等子线程停止,主线程就先停止了。

主线程停止后,整个程序停止,子线程在没有printf的时候就被结束了。

结论:不是你没有看到结果,而是在子线程printf("..................\n")之前整个程序就已经停止了。

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

#define FALSE -1

#define TRUE 0

void *shuchu( void *dumy )

{

int j

printf("..................\n")

}

int main()

{

int i = 0

int rc = 0

int ret1

pthread_t p_thread1

if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n")

printf("[%d]\n",p_thread1)

pthread_join(p_thread1, NULL)

return TRUE

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12006221.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存