是否调用 shmget 或 shmat 初始化数据?

Do calls to shmget or shmat initialize data?

在c中使用共享内存时,是否可以先创建数据,然后将其附加到共享段(类似于初始化)?或者我必须先创建共享段然后将数据附加到它吗?

我确实认为这两种方式都是正确的,但我不确定在调用 shmgetshmat 期间到底发生了什么。执行这些调用中的任何一个,初始化附加数据?

is it possible to first create data and then attach it to the shared segment?

没有

Or must I first create the shared segment and then attach data to it?

是的 - 你得到那块内存,然后你 write/place 里面的数据。

shmat() 为您提供指向共享段的指针。当您将其分配给程序中的现有指针时,该指针指向这块内存。

Do any of these calls, initialize the attached data?

创建新的共享内存段时,其中的所有字节都将初始化为零。附加已创建的段时,您将获得之前放置在该段中的任何内容。