im trying to implement a basic program which writes an array of strings in shared memory and then it reads it from there. I am constantly taking a segmentation fault. I cant really tell what is wrong ( i have really messed it up ) because i am using shared memory for first time. Any help would be appreciated , thanks!
code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
int shmid;
int i = 0;
key_t key = 123;
char (*array)[100][50];
shmid = shmget(key, 100 * 50, IPC_CREAT | 0666);
array = (char *)shmat(shmid, 0, 0);
// array = malloc(sizeof(int)*count);
for (i = 0; i < 10; i++)
{
strcpy(array[i], "arrayvalues");
}
for (i = 0; i < 10; i++)
{
printf("\n%s---\n", array[i]);
}
printf("\nWritting to memory succesful[+]\n");
shmid = shmget(key, 1024, IPC_CREAT | 0666);
array = (char *)shmat(shmid, 0, 0);
for (i = 0; i < 9; i++)
{
printf("\n%s---\n", array[i]);
}
printf("\nRead to memory succesful[+]\n");
shmdt((void *)array);
// shmctl(shmid, IPC_RMID, NULL);
return 0;
}
EDIT: it only saves the array[0] value in shared memory.
gcc, at a minimum use:-Wall -Wextra -pedanticI also use:-Wconversion -std=gnu11)