0

I want to create a program to store data in a 2D array. This 2D array should be created in the shared memory.

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



key_t key;
int shmBuf1id;
int *buf1Ptr;

main(int argc, int *argv[])
{
    createBuf1();

}

createBuf1()
{
  key = ftok(".",'b');
  shmBuf1id = shmget(key,sizeof(int[9][9]),IPC_CREAT|0666);

  if(shmBuf1id == -1 )
  {
    perror("shmget");
    exit(1);
  }

  else
  {
    printf("Creating new Sahred memory sement\n");
    buf1Ptr[3] = shmat(shmBuf1id,0,0);
    if(buf1Ptr == -1 )
    {
      perror("shmat");
      exit(1);
    }


  }

}

But when I run this program it gives a segmentation fault(Core dumped) error. Have I created the 2D array in the shared memory correctly?

2
  • You never initialized buf1Ptr before you assigned to buf1Ptr[3]. Commented May 3, 2017 at 15:22
  • There is no 2D array and no pointer to one (or a 1D array, which is the idenomatic way). int * is a pointer to int. Commented May 3, 2017 at 15:33

2 Answers 2

3

First, int *buf1Ptr is a pointer to int. In your case you want a pointer to a 2-dimensional array of integers, so you should declare it as:

int (*buf1Ptr)[9];

Then you need to initialize the pointer itself:

buf1Ptr = shmat(shmBuf1id,0,0);

Now you can access your array through buf1Ptr (ie. buf1Ptr[0][0] = 1). Here's a complete working version of your program:

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

key_t key;
int shmBuf1id;
int (*buf1Ptr)[9];

void
createBuf1()
{
  key = ftok(".",'b');
  shmBuf1id = shmget(key,sizeof(int[9][9]),IPC_CREAT|0666);

  if(shmBuf1id == -1 )
  {  
    perror("shmget");
    exit(1);
  }
  else
  {  
    printf("Creating new Sahred memory sement\n");
    buf1Ptr = shmat(shmBuf1id,0,0);
    if(buf1Ptr == (void*) -1 )
    {  
      perror("shmat");
      exit(1);
    }
  }  
}

int
main(int argc, int *argv[])
{
    createBuf1();
    return 0; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please tell me why did you use int (*buf1Ptr)[9] to initialize the 2d array? Shouldn't it be int (*buf1Ptr)[9][9] ? (Since it's 2d)
It allows you to use code like buf1Ptr[0][0] to access the cells instead of (*buf1Ptr)[0][0]. It works because an array and a pointer is more or less the same thing in C, so the pointer itself is the first dimension.
-1

you forgot allocate memory:

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



key_t key;
int shmBuf1id;
int *buf1Ptr;

int main(int argc, char *argv[])
{
    createBuf1();

}

createBuf1()
{
  key = ftok(".",'b');
  shmBuf1id = shmget(key,sizeof(int[9][9]),IPC_CREAT|0666);

  if(shmBuf1id == -1 )
  {
    perror("shmget");
    exit(1);
  }

  else
  {
    printf("Creating new Sahred memory sement\n");
    int buf1Ptr[4];
    buf1Ptr[3] = shmat(shmBuf1id,0,0);
    if(buf1Ptr == -1 )
    {
      perror("shmat");
      exit(1);
    }


  }

}

2 Comments

Can you explain it further, please? Why did you use int buf1Ptr[2]?
It was an mistake, i wanted to use [4] I used buf1Ptr[4] because you wanted use the fourth position in memory. So it need to be an array with greater than or equal 4 positions

Your Answer

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