且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个共享内存中的两个变量

更新时间:2023-02-22 12:56:37

显然(阅读手册)shmat 为您提供了一块内存,大小为 2*sizeof(int).

Apparently (reading the manual) shmat gets you here a single block of memory, of size 2*sizeof(int).

如果是这样,那么您只需调整指针即可:

If so, then you can just adjust the pointer:

int *a, *b;
a = shmat(shmid, NULL, 0);
b = a+1;

此外,这里的转换是错误的,出于此处列出的原因(虽然问题是关于 malloc代码>,同样的参数适用)

Also, casting here is wrong, for reasons listed here (while the question is about malloc, the same arguments apply)