当前位置: 技术问答>linux和unix
共享内存问题,只能得到int值,不能得到char*的值?
来源: 互联网 发布时间:2015-08-28
本文导语: 我定义了一个结构,结构中有int,char *, 但是程序可以从共享内存中取得int值,却不可以得到 char * 的值。 下面是程序。 === linux_s.c ==== /* Includes */ #include #include #include #include #include #include #in...
我定义了一个结构,结构中有int,char *, 但是程序可以从共享内存中取得int值,却不可以得到 char * 的值。
下面是程序。
=== linux_s.c ====
/* Includes */
#include
#include
#include
#include
#include
#include
#include
typedef struct
{
int id;
char *name;
} student;
main()
{
/* local variables */
int ret= 0;
key_t key;
int i;
int shm_id;
student *com_student;
key = ftok(".",'1');
char *tmp;
/* create a share memory if not exist */
if ((shm_id = shmget(key ,sizeof(student),IPC_CREAT|IPC_EXCL|0666)) == -1)
{
/* share memory has been created */
if ((shm_id = shmget(key , sizeof(student),0)) == -1)
{
printf("error = %dn", errno);
return (ret);
}
}
com_student = (student *) shmat(shm_id, (student *) 0, 0);
com_student->name = (char *)malloc(sizeof(char)*20);
memset(com_student->name,'',20);
com_student->id= 110;
sprintf(com_student->name,"%s","Liuyongsh");
printf("student's id=%dn", com_student->id);
printf("stduent's name=%xn", com_student->name);
printf("stduent's name=%sn", com_student->name);
if(shmdt(com_student)==-1)
perror(" detach error ");
}
========linux_c.c=======
#include
#include
#include
#include
#include
#include
#include
typedef struct
{
int id;
char *name;
} student;
main()
{
/* local variables */
int ret= 0;
key_t key;
int i;
int shm_id;
int found = 0;
student *com_student;
key = ftok(".",'1');
/* share memory has been created */
if ((shm_id = shmget(key , sizeof(student),0)) == -1)
{
printf("error = %dn", errno);
return (ret);
}
com_student = (student *) shmat(shm_id, (student *) 0, 0);
printf("student's id=%dn", com_student->id);
printf("stduent's name=%xn", com_student->name);
printf("stduent's name=%sn", com_student->name);
/* kill share memory */
//shmctl(shm_id,IPC_RMID,0);
if(shmdt(com_student)==-1)
perror(" detach error ");
exit(0);
}
屏幕输出是这样的:
[liuys@dispam megshm]$ ./linux_s
student's id=110
stduent's name=80498f8
stduent's name=Liuyongsh
[liuys@dispam megshm]$ ./linux_c
student's id=110
stduent's name=80498f8
stduent's name= //地址一样,这里却没有输出。
拜托各位大哥指点一下。
下面是程序。
=== linux_s.c ====
/* Includes */
#include
#include
#include
#include
#include
#include
#include
typedef struct
{
int id;
char *name;
} student;
main()
{
/* local variables */
int ret= 0;
key_t key;
int i;
int shm_id;
student *com_student;
key = ftok(".",'1');
char *tmp;
/* create a share memory if not exist */
if ((shm_id = shmget(key ,sizeof(student),IPC_CREAT|IPC_EXCL|0666)) == -1)
{
/* share memory has been created */
if ((shm_id = shmget(key , sizeof(student),0)) == -1)
{
printf("error = %dn", errno);
return (ret);
}
}
com_student = (student *) shmat(shm_id, (student *) 0, 0);
com_student->name = (char *)malloc(sizeof(char)*20);
memset(com_student->name,'',20);
com_student->id= 110;
sprintf(com_student->name,"%s","Liuyongsh");
printf("student's id=%dn", com_student->id);
printf("stduent's name=%xn", com_student->name);
printf("stduent's name=%sn", com_student->name);
if(shmdt(com_student)==-1)
perror(" detach error ");
}
========linux_c.c=======
#include
#include
#include
#include
#include
#include
#include
typedef struct
{
int id;
char *name;
} student;
main()
{
/* local variables */
int ret= 0;
key_t key;
int i;
int shm_id;
int found = 0;
student *com_student;
key = ftok(".",'1');
/* share memory has been created */
if ((shm_id = shmget(key , sizeof(student),0)) == -1)
{
printf("error = %dn", errno);
return (ret);
}
com_student = (student *) shmat(shm_id, (student *) 0, 0);
printf("student's id=%dn", com_student->id);
printf("stduent's name=%xn", com_student->name);
printf("stduent's name=%sn", com_student->name);
/* kill share memory */
//shmctl(shm_id,IPC_RMID,0);
if(shmdt(com_student)==-1)
perror(" detach error ");
exit(0);
}
屏幕输出是这样的:
[liuys@dispam megshm]$ ./linux_s
student's id=110
stduent's name=80498f8
stduent's name=Liuyongsh
[liuys@dispam megshm]$ ./linux_c
student's id=110
stduent's name=80498f8
stduent's name= //地址一样,这里却没有输出。
拜托各位大哥指点一下。
|
可以定义成
typedef struct
{
int id;
char name[20];
} student;
typedef struct
{
int id;
char name[20];
} student;