gpt4 book ai didi

c - linux c 共享内存 :why the order of contents are opposite when write and read

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:43 25 4
gpt4 key购买 nike

<分区>

我想在共享内存中使用 int 数组,在将 1,2,3 写入其中后,我希望这样读取它:1,2,3。但是我读取了这个:3,2,1。我没有知道为什么

write code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);

*gIn++=10;
*gIn++=20;
*gIn++=30;
sleep(10);
return 0;
}

read code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);

printf("%d|%d|%d\n",*gIn++,*gIn++,*gIn++);
return 0;
}

我期望读取过程的输出是10|20|30,但实际输出是30|20|10,很奇怪,不知道为什么

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com