据我所知,数组被分配了连续的内存块。我写了一个程序来测试我的理解。它基本上附加了共享内存段。这是代码:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
int* shared_memory;
struct shmid_ds shmbuffer;
int segment_size;
const int
shared_segment_size = 4096; //one page size
int *results;
int cpid=0;
int sum=0;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. Results now contain the starting address */
results =shmat (segment_id, 0, 0);
printf("%p\n",&results);
printf("%p\n",&results[1]);
printf("%p\n",&results[2]);
printf("%p\n",&results[3]);
return 0;
}
这是输出:
0x7fffe4d62e40
0x7f6603987004
0x7f6603987008
0x7f660398700c
有人可以向我解释为什么结果[1]的地址是:
0x7f6603987004
假设结果[0]的地址是:
0x7fffe4d62e40
不应该是0x7fffe4d62e44吗? 我错过了一些至关重要的东西吗?