当前位置: 技术问答>linux和unix
共享内存的问题~
来源: 互联网 发布时间:2015-11-24
本文导语: 这个为什么读不出来呢~~ #include #include #include typedef struct student { char name[10]; int nID; }STU; int main() { STU s1; strcpy(s1.name,"wang"); s1.name[4]=''; s1.nID = 1001; STU s2; strcpy(s2.name,"zhang"); s1.name[5]=''; s1.nID = 1002...
这个为什么读不出来呢~~
#include
#include
#include
typedef struct student
{
char name[10];
int nID;
}STU;
int main()
{
STU s1;
strcpy(s1.name,"wang");
s1.name[4]='';
s1.nID = 1001;
STU s2;
strcpy(s2.name,"zhang");
s1.name[5]='';
s1.nID = 1002;
int segment_id;
char *shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int share_segment_size =0X5400;
segment_id = shmget(IPC_PRIVATE,share_segment_size,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
shared_memory = (char *) shmat(segment_id,0,0);
printf("shared memory attached at address %p]n",shared_memory);
shmctl(segment_id,IPC_STAT,&shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf("segment size :%dn",segment_size);
memcpy(shared_memory,&s1,sizeof(s1));
shmdt(shared_memory);
shared_memory =(char *) shmat(segment_id,(void *)0X500000,0);
printf("shared memory attached at address %pn",shared_memory);
printf("%s",(STU)shared_memory->name);
printf("%d",(STU)shared_memory->nID);
shmctl(segment_id,IPC_RMID,0);
return 0;
}
#include
#include
#include
typedef struct student
{
char name[10];
int nID;
}STU;
int main()
{
STU s1;
strcpy(s1.name,"wang");
s1.name[4]='';
s1.nID = 1001;
STU s2;
strcpy(s2.name,"zhang");
s1.name[5]='';
s1.nID = 1002;
int segment_id;
char *shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int share_segment_size =0X5400;
segment_id = shmget(IPC_PRIVATE,share_segment_size,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
shared_memory = (char *) shmat(segment_id,0,0);
printf("shared memory attached at address %p]n",shared_memory);
shmctl(segment_id,IPC_STAT,&shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf("segment size :%dn",segment_size);
memcpy(shared_memory,&s1,sizeof(s1));
shmdt(shared_memory);
shared_memory =(char *) shmat(segment_id,(void *)0X500000,0);
printf("shared memory attached at address %pn",shared_memory);
printf("%s",(STU)shared_memory->name);
printf("%d",(STU)shared_memory->nID);
shmctl(segment_id,IPC_RMID,0);
return 0;
}
|
printf("%s",(STU)shared_memory->name);
printf("%d",(STU)shared_memory->nID);
改成
printf ("%s", ((STU*)shared_memory)->name);
printf ("%d", ((STU*)shared_memory)->nID);
printf("%d",(STU)shared_memory->nID);
改成
printf ("%s", ((STU*)shared_memory)->name);
printf ("%d", ((STU*)shared_memory)->nID);