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?
buf1Ptrbefore you assigned tobuf1Ptr[3].int *is a pointer toint.