'program received signal sigsegv segmentation fault on using a pointer of a struct pointing to a structure placed on a shared memory

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define PORT 8080
#define SHM_SIZE 1024*(512+4)+100*((4*2)+(1024*2))+(4*4)

//---modes-----//
#define NOAUTO "NOAUTO"
#define AUTO "AUTO"

//---commands---//
#define SEND "SEND"
#define FETCH "FETCH"
#define FETCHIF "FETCHIF"
#define QUIT "QUIT"
#define EOF2 "EOF"

typedef char message[512];

typedef struct {
    bool is_active;
    int start_index;
    int end_index;
    bool indices[1024];
}circular_queue;

typedef struct {
    message message_list[1024];
    int last_index;
    circular_queue connections[100];
    int last_conn_index;
    int remained_read[1024];
    int num_conn;
    int queue_start;
}multicast_queue;


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

    //-------------------connect to server-----------------//
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char *hello = "Hello from client";
    char shmid_s[512] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // Convert IPv4 and IPv6 addresses from text to binary form
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }
    read( sock , shmid_s, 512); //-----read shared memory information over socket-----//
    int shmid = atoi(shmid_s);
    printf("%d\n",shmid );

    //---------------------attach to shared memory---------------------------//


    if(shmid == -1)
    {
        perror("shmget");
        exit(1);
    }
    multicast_queue *multicast_queue = shmat(shmid, (void*)0, 0);
    if(multicast_queue == (void*)(-1))
    {
        perror("shmat");
        exit(1);
    }
    int conn_no=multicast_queue->last_conn_index;
    int read_index=multicast_queue->queue_start;

    char *mode = NOAUTO;
    printf("%s\n",mode);
    char command[7];
    memset(command,0,7);
    message messagebody;

    while(scanf("%s",command))
    {
        if(!strcmp(command,SEND))
        {
            printf("Enter the message to send: ");
            scanf("%s", messagebody);
            printf("%s %s\n", command, messagebody);
        }
        else if(!strcmp(command,FETCH))
        {
            message f_message;
            memcpy(f_message, multicast_queue->message_list[read_index],512);
            multicast_queue->remained_read[read_index]--;
            if(multicast_queue->remained_read[read_index] == 0)
            {
                multicast_queue->queue_start = (multicast_queue->queue_start+1)%1024;
                multicast_queue->connections[conn_no].start_index = (multicast_queue->connections[conn_no].start_index+1)%1024;
            }
            read_index++;
            printf("%s\n", f_message);

        }
    }


    return 0;
}

This is a client code piece of a UNIX domain stream socket project. The shared memory is created by the server code and shmid is send through the socket. Then, to use the shared memory, a pointer of the same type with the constructed data structure over the shared memory is set. Using this pointer the shared memory is attached to this client process. I get a segmentation error whenever i try to access a member of the multicast_queue which is attached to the shared memory (int conn_no=multicast_queue->last_conn_index;). I run this program on a virtual machine Ubuntu. I don't know what the reason behind getting this error is. I also increased the memory size for Ubuntu but it didn't work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source