1

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.

8
  • 1
    The irony that your name is segfault. Commented Apr 20, 2017 at 16:36
  • 1
    @AjayBrahmakshatriya I am really bad in memory management! :) Commented Apr 20, 2017 at 16:37
  • 1
    Start by checking shmget and shmat return values for errors (read the man pages for details). Are the return values ok? (In general, it's pointless to debug bugs on code which lacks basic error checking.) Commented Apr 20, 2017 at 16:56
  • @hyde i've tried but i get segmentation fault on shmget so i cant really check what it returns.. Commented Apr 20, 2017 at 17:01
  • the posted code fails to cleanly compile! When compiling, always enable all the warnings, then fix those warnings. (for gcc, at a minimum use: -Wall -Wextra -pedantic I also use: -Wconversion -std=gnu11 ) Commented Apr 21, 2017 at 15:03

1 Answer 1

3

It seems the segmentation fault is not related to using shared memory. Actually what you are declaring here (*array)[100][50] is a pointer to a 2D array. But while accessing the same you are treating it as a normal 2D array variable. Thus, if you access it like (*array)[i] then there is no memory violation.

*array points to the 2D array while array is just the location of the pointer to the array. You can try populating the array like (*array)[i] and then try to print array[i] in a loop. The 1st element i.e array[0] will be valid as the index 0 will mean nothing but printing *array. But from index 1 onwards it will try to access locations *(array+1) which may not be valid locations. This causes the segmentation fault. But (*array)[i] means accessing location (*array) + 1 which is valid, as arrays are allocated in contagious memory locations

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome! Updated answer about saving array[0] value

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.