本想试验一下,动态分配的内存内容在传给另一个线程后,如何显示。
背景是tcmd,free()后出段错误,我就自己写了一个小程序,试验一下。
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
typedef struct {
char buff[30];
void *data;
}OP_TCMD;
void* thread1(char *buff1)
{
int i;
char buff[30];
memset(buff, 0, 30);
memcpy(buff, buff1,strlen(buff1));
printf("begin buff: %s ,len = %d \n",buff, strlen(buff));
for(i=0; i<10;i++)
{
printf("buff[%d]= %d\n", i, buff[i]);
}
printf("end\n");
}
void* thread2(OP_TCMD *tcmd)
{
int i;
char buff[30];
memcpy(buff, tcmd->data, 30);
printf("buff:%s\n", buff);
printf("data addr:%08x \n ", &tcmd->data);
//printf("begin, %d %s \n",strlen(tcmd->data), tcmd->data);
for(i=0; i<10;i++)
{
printf("buff[%d]= %04x\n", i, buff[i]);
}
printf("end\n");
}
int main()
{
OP_TCMD *tcmd;
OP_TCMD tcmd_get;
tcmd =&tcmd_get;
pthread_t thread_id;
int state;
char *buff="hello world , icommonweal";
char *dynamic= (char *)malloc(30);
memset(dynamic, 0, 30);
strncpy(dynamic, buff, strlen(buff));
memset(tcmd_get.buff, 0, 30);
strncpy(tcmd_get.buff, "1234", 4);
printf("dynamic buff: %s len= %d\n",dynamic, strlen(dynamic));
tcmd_get.data = dynamic ;
printf("dynamic:%08x \n ", &dynamic);
/*create thread */
state=pthread_create(&thread_id, NULL,(void *)&thread1 ,(void*)dynamic);
if(state<0)
{
printf("create thread failed\n");
}
else
{
// pthread_detach(thread_id);
printf("create thread sucess\n");
}
free(dynamic);
pthread_join(thread_id, NULL);
return 0;
}
|
在state=pthread_create(&thread_id, NULL,(void *)&thread1 ,(void*)dynamic);
后,本想看到它会把动态分配内存中的东西打印出来,却显示
dynamic buff: hello world , icommonweal len= 25
dynamic:bfa858a0
create thread sucess
begin buff: ,len = 0
buff[0]= 0
buff[1]= 0
buff[2]= 0
buff[3]= 0
buff[4]= 0
buff[5]= 0
buff[6]= 0
buff[7]= 0
buff[8]= 0
buff[9]= 0
end
|
怎么是len=0,我想不懂,难道动态内存分配的东西传入不到线程中去??
我也写了另一个线程thread2,把数据指针放入到tcmd_get.data = dynamic ; state=pthread_create(&thread_id, NULL,(void *)&thread2 ,(void*)tcmd);
执行显示也是
root@:/home/qing/lib# ./a.out
dynamic buff: hello world , icommonweal len= 25
dynamic:bfd73380
create thread sucess
buff:
data addr:bfd733b0
buff[0]= 0000
buff[1]= 0000
buff[2]= 0000
buff[3]= 0000
buff[4]= 006f
buff[5]= 0020
buff[6]= 0077
buff[7]= 006f
buff[8]= 0072
buff[9]= 006c
end
|
buff为空,
buff[0]= 0000
buff[1]= 0000
buff[2]= 0000
buff[3]= 0000
真是奇怪了,怎么想也不明白,但只要是把
tcmd_get.data = dynamic ;改为tcmd_get.data = buff;
state=pthread_create(&thread_id, NULL,(void *)&thread2 ,(void*)tcmd);就能正确显示:
root@:/home/qing/lib# ./a.out
dynamic buff: hello world , icommonweal len= 25
dynamic:bfac78e0
create thread sucess
buff:hello world , icommonweal
data addr:bfac7910
buff[0]= 0068
buff[1]= 0065
buff[2]= 006c
buff[3]= 006c
buff[4]= 006f
buff[5]= 0020
buff[6]= 0077
buff[7]= 006f
buff[8]= 0072
buff[9]= 006c
end
|
能正确的显示。
同样,把 state=pthread_create(&thread_id, NULL,(void *)&thread1 ,(void*)dynamic);
改为 state=pthread_create(&thread_id, NULL,(void *)&thread1 ,(void*)buff);
也能正确显示:
root@:/home/qing/lib# ./a.out
dynamic buff: hello world , icommonweal len= 25
dynamic:bf816630
create thread sucess
begin buff: hello world , icommonweal ,len = 25
buff[0]= 104
buff[1]= 101
buff[2]= 108
buff[3]= 108
buff[4]= 111
buff[5]= 32
buff[6]= 119
buff[7]= 111
buff[8]= 114
buff[9]= 108
end
|
难道真是线程不能处理动态内存的内容?、
最终发现错误的地方:
free(dynamic); pthread_join(thread_id, NULL);
|
在主线程free()后,开启的线程当然不后显示正确。如果两句倒一下位置就可以正确显示了。
pthread_join(thread_id,NULL);
|
阅读(2367) | 评论(1) | 转发(1) |