1

I'm trying to create a piece of shared memory that holds an array of structs. In my current code when i run it i get a segmentation fault. I think I may need to use memcpy but am severely stuck at the moment. Any help would be mch appreciated...

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <unistd.h>
#include "header.h"


int main()
{
    key_t key = 1234;
    int shmid;
    int i = 1;

    struct companyInfo * pdata[5];

    strcpy(pdata[0]->companyName,"AIB");
    pdata[0]->sharePrice = 11.02;
    strcpy(pdata[1]->companyName,"Bank Of Ireland");
    pdata[1]->sharePrice = 10.02;
    strcpy(pdata[2]->companyName,"Permanent TSB");
    pdata[2]->sharePrice = 9.02;
    strcpy(pdata[3]->companyName,"Bank Od Scotland");
    pdata[3]->sharePrice = 8.02;
    strcpy(pdata[4]->companyName,"Ulster Bank");
    pdata[4]->sharePrice = 7.02;



    int sizeOfCompanyInfo = sizeof(struct companyInfo);

    int sizeMem = sizeOfCompanyInfo*5;

    printf("Memory Size: %d\n", sizeMem);

    shmid = shmget(key, sizeMem, 0644 | IPC_CREAT);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    *pdata = (struct companyInfo*) shmat(shmid, (void*) 0, 0);
    if(*pdata == (struct companyInfo*) -1)
    {
        perror("schmat error");
        exit(1);
    }

    printf("name is %s and %f . \n",pdata[0]->companyName,pdata[0]->sharePrice);

    exit(0);

}

the header.h file is as follows...

struct companyInfo
{
    double sharePrice;
    char companyName[100];
}; 
4
  • Where in your code does the seg fault occur? Can you step through with a debugger or even just put printf() statements in to see at least where the crash occurs? Commented Mar 11, 2012 at 22:49
  • It doesnt seem to like me assigning values to the array, when i strcpy into pdata[1] the segmentation fault occurs. i found it by throwing in printfs Commented Mar 11, 2012 at 22:55
  • Then it sounds like pdata[1] wasn't initialized properly. Fortunately, there are a couple answers below that explain how to fix that. Commented Mar 11, 2012 at 22:56
  • You have two answers that point out the problem. I would accept hmjd's answer because it is more complete and provides code. Commented Mar 11, 2012 at 22:58

2 Answers 2

3
struct companyInfo * pdata[5];

contains an array of 5 uninitialized pointers. You need too allocate memory for each element in the array before using them:

for (int i = 0; i < 5; i++)
{
    pdata[i] = malloc(sizeof(companyInfo));
}

or just declare an array of struct companyInfo as there does not appear to be any need for dynamic allocation:

struct companyInfo pdata[5];
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. using malloc helped a lot and got rid of the segmentation fault.
Unfortunately this doesn't make the shared memory work.. stackoverflow.com/questions/9660503/…
3

pdata is a table of pointers so you need to create each struct companyInfo using malloc before being able to access them.

Comments

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.