1

I want to create a 2D array of char and share it through shared memory, but when I try to write in my shared memory I have segmentation fault, what can I do to write a string in my array

 int main {
            int j;
        char** shm = malloc(30 * sizeof(char *));

          for (int index=0;index<30;++index)
          {
            shm[index] =malloc(100 * sizeof(char));
          }

          int sh_id;
          int i;
          size_t sizeMatrix = sizeof_dm(30,100,sizeof(char));

          //creation ou ouverture de la file de segment partag�e
          key_t keyfile = ftok("keyFileIpc.txt",10);

          if((sh_id = shmget(keyfile,sizeMatrix,IPC_CREAT|0666)) == -1) {
            perror("shmget");
            exit(1);
          }

          //on attache le segment au shm
          if ((void*)(shm =(char**)shmat(sh_id, NULL, 0)) == (void *) -1) {
            perror("shmat");
            exit(1);
          }
          //i can't write in here and i don't know why
          for (j=0;j<30;j++)
          {
            strcpy(shm[j],"i'm here");
          }
          printf("here\n");
    return 0;

}
4
  • 2
    shm =(char**)shmat(...) reassigns shm, making you lose the earlier allocations and give you a memory leak. Commented Nov 26, 2019 at 10:11
  • so if i allocate after the shmat(...) would my shm still be in my shared memory ? Commented Nov 26, 2019 at 10:15
  • More importantly, shm =(char**)shmat(...) sets shm to point to some pointers that have been stored by another process. Those pointers have addresses in that other process’ address space. They are generally not valid in this process’ address space. Commented Nov 26, 2019 at 10:15
  • Also, when you ask questions like this, you should provide a minimal reproducible example, including complete source code for two programs that can be compiled and run to demonstrate the problem. Commented Nov 26, 2019 at 10:18

1 Answer 1

2

The problem is that shm is a jagged array, not a contiguous memory area which is what you need for your shared memory.

In memory shm would look something like

+--------+     +-----+
| shm[0] | --> | ... |
+--------+     +-----+
| shm[1] | --> | ... |
+--------+     +-----+
| shm[2] | --> | ... |
+--------+     +-----+
| ...    |
+--------+

That is, each element of shm is a pointer.

For a "proper" 2d array each element needs to be an array:

+-----------+-----------+-----------+-----+
| shm[0][0] | shm[0][1] | shm[0][2] | ... |
+-----------+-----------+-----------+-----+
| shm[1][0] | shm[1][1] | shm[1][2] | ... |
+-----------+-----------+-----------+-----+
| shm[2][0] | shm[2][1] | shm[2][2] | ... |
+-----------+-----------+-----------+-----+
| ...                                     |
+-----------+-----------+-----------+-----+

As a simple solution you can use an algorithm to treat the contiguous area returned by shmat as an emulated 2d array:

char *shm;

// ...

for (j=0;j<30;j++)
    strcpy(&shm[j * 100], "I'm here");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.