共享内存,用于共享相同结构的数组

发布于 2024-12-13 17:29:26 字数 851 浏览 4 评论 0原文

好的,先看看,谢谢这个地方:) 我想创建一块共享内存来存储30个相同类型的结构,但是我在编译时遇到错误。

struct nota
{
    int n;
    char text[30];
    char titulo[100];
    char login[20];
}

main(){
     int shmID;
     struct nota *evernota;
     struct nota um;

     shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

     evernota = shmat(shmID, NULL, 0);

     evernota[0] = &um;  //Declaring one note here to the first position of shm..
     evernota[20] = &um;  //Declaring the same note on the 20 position of shm..

     printf("o meu int é %d\n",evernota[0]->n);  //Here I would get the int n of each structure, that should be 0, because its not initialized..
     printf("o meu int é %d\n",evernota[20]->n);  //and there the same n, because I use the same structure on position 0 and 20..  
}

但是我有编译错误,有人看到问题出在哪里吗? 非常感谢!

Okay look first thanks for this place :)
I would like to create a piece of shared memory to store 30 structures of the same type, but I am getting errors on compile..

struct nota
{
    int n;
    char text[30];
    char titulo[100];
    char login[20];
}

main(){
     int shmID;
     struct nota *evernota;
     struct nota um;

     shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

     evernota = shmat(shmID, NULL, 0);

     evernota[0] = &um;  //Declaring one note here to the first position of shm..
     evernota[20] = &um;  //Declaring the same note on the 20 position of shm..

     printf("o meu int é %d\n",evernota[0]->n);  //Here I would get the int n of each structure, that should be 0, because its not initialized..
     printf("o meu int é %d\n",evernota[20]->n);  //and there the same n, because I use the same structure on position 0 and 20..  
}

But I have compile errors, someone see where is the problem??
Thanks alot in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-12-20 17:29:26

对于初学者来说,您需要包含:

#include <stdio.h>
#include <string.h>
#include <sys/shm.h>

然后您需要在结构定义后添加分号:

struct nota {
    ...
};

添加标志IPC_EXCL,如果内存密钥已在使用中,则使shmget失败(使用< code>IPC_PRIVATE 而不是 1009 以保证内存段是新的)。然后检查 shmID != -1 就很好了,要复制引用,请使用 memcopy

memcpy(&evernota[0], &um, sizeof(struct nota));  
memcpy(&evernota[20], &um, sizeof(struct nota));

关于 printf,使用 <代码>.而不是<代码>->。它们会打印垃圾数据,它们不会在分配时自动初始化为 0,您必须自己完成。

well for starters you need the includes:

#include <stdio.h>
#include <string.h>
#include <sys/shm.h>

then you need the semicolon after struct definition:

struct nota {
    ...
};

Add the flag IPC_EXCL, makes shmget fail if the memory key is already in use (use IPC_PRIVATE instead of 1009 to guarantee the memory segment is new). Then a check on shmID != -1 would be nice, and to copy the references use memcopy:

memcpy(&evernota[0], &um, sizeof(struct nota));  
memcpy(&evernota[20], &um, sizeof(struct nota));

about the printfs, use the . instead of ->. They will print garbage data, they're not automatically 0-initialized on allocation, you have to do it by yourself.

浅听莫相离 2024-12-20 17:29:26

struct nota类型的变量um未初始化。您期望将什么打印到标准输出?

检查丢失的标头,它们至少应该是:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

要初始化数组的一个元素,请使用 memcpy:

memcpy(&evernota[0], &um, sizeof(struct nota));

检查系统函数的返回值始终是一种很好的方式:

   if ((shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT) < 0) {
        perror("shmget");
        exit(1);
   }

Variable um of struct nota type is not initialized. What do you expect to be printed to stdout?

Check missed headers, they should be at least:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

To initialize one element of an array use memcpy:

memcpy(&evernota[0], &um, sizeof(struct nota));

It's always good style to check return values of system function:

   if ((shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT) < 0) {
        perror("shmget");
        exit(1);
   }
一笑百媚生 2024-12-20 17:29:26

好的,这段代码有很多错误。您至少需要以下内容:

 #include <sys/shm.h>
 #include <stdio.h>

第二个问题是您根本没有初始化 um 或 Evernote。这意味着,例如 evernote[0]->n 将包含垃圾数据。所以你至少应该有,例如

um.n = 1;

现在出现了将 um 复制到共享内存段的问题。您需要将 um 结构定义的内存内容复制到evernota 数组中。为此:

 memcpy(&evernota[0], &um, sizeof(struct nota));  
 memcpy(&evernota[20],&um,sizeof(struct nota));  

注意:memcpystring.h 中定义。

现在最后,要打印 evernota[0] 中字段 n 的内容,您只需要使用点运算符,即

 printf("o meu int é %d\n",evernota[20].n);  

我认为这就是一切。

编辑:下面的代码仍然会给您带来段错误吗?

   #include <sys/shm.h>
   #include <string.h>
   #include <stdio.h>

   struct nota {
     int n;
     char text[30];
     char titulo[100];
    char login[20];
  };

     int main(){

        int shmID;
        struct nota *evernota;
        struct nota um;

        um.n = 1;
        shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

        evernota = shmat(shmID, NULL, 0);

        memcpy(&evernota[0], &um, sizeof(struct nota));  
        memcpy(&evernota[20],&um,sizeof(struct nota));  

        printf("o meu int é %d\n",evernota[0].n);  
        printf("o meu int é %d\n",evernota[20].n); 

        return 0; 
      } 

Ok, there is a lot wrong with this code. You at least need the following includes:

 #include <sys/shm.h>
 #include <stdio.h>

The second problem is that you are not initialising um or evernote at all. This means that for example, evernote[0]->n will contain garbage data. So you should at least have, for example

um.n = 1;

Now comes the problem of copying um to the shared memory segment. You need to copy the contents of the memory defined by the um struct into the evernota array. To do this:

 memcpy(&evernota[0], &um, sizeof(struct nota));  
 memcpy(&evernota[20],&um,sizeof(struct nota));  

NB: memcpy is defined in string.h.

Now finally, to print the contents of the field n in evernota[0] you need only use the dot operator i.e.

 printf("o meu int é %d\n",evernota[20].n);  

I think thats everything.

EDIT: Does the code below still give you segfaults?

   #include <sys/shm.h>
   #include <string.h>
   #include <stdio.h>

   struct nota {
     int n;
     char text[30];
     char titulo[100];
    char login[20];
  };

     int main(){

        int shmID;
        struct nota *evernota;
        struct nota um;

        um.n = 1;
        shmID = shmget(1009, sizeof(struct nota)*30 , 0666 | IPC_CREAT);

        evernota = shmat(shmID, NULL, 0);

        memcpy(&evernota[0], &um, sizeof(struct nota));  
        memcpy(&evernota[20],&um,sizeof(struct nota));  

        printf("o meu int é %d\n",evernota[0].n);  
        printf("o meu int é %d\n",evernota[20].n); 

        return 0; 
      } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文