shared memory array

This is a discussion on shared memory array within the C Programming forums, part of the General Programming Boards category; I am getting a Segmentation fault(core dump) after i try to put my values into the shared array. Any Ideas? ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    shared memory array

    I am getting a Segmentation fault(core dump) after i try to put my values into the shared array. Any Ideas?
    Code:
    #include <stdio.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include "Fib.h"
    
    int main(int argc, char *argv[]){
        
    int segment_id;
    int *shared_memory[10];
    const int size = 9096;
        
        segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
    
        printf("argc = %d", argc);
        pid_t pid;
        pid=fork();
    
        if(pid == 0){ 
            /*child process*/    
            /*writes child fib array*/        
            int fibArray[10];
            int value = 10;
            fibArray[0]=1;
            fibArray[1]=2;    
            int j;
            for(j=2;j<value+1;j++){
                fibArray[j]=fibArray[j-1]+fibArray[j-2];            
            }    
            int i;        
            for(i=0; i<value+1; i++){
                printf("\nChild %d", i+1);
                printf("= %d",fibArray[i]);
            }
            /*child fib array ends*/
    
            /*writes the shared fib array*/
            *shared_memory[0]=1;
            *shared_memory[1]=2;
            int k;
            for(k=2;k<value;k++){
                *shared_memory[k] = *shared_memory[k-1] + *shared_memory[k-2];    
                printf("\n%d", k);
            }    
            int l;        
            for(l=0; l<value; l++){
                printf("\nChild shared %d", l+1);
                printf("= %d", *shared_memory[l]);
            }
            return 0;
        }
        else if(pid > 0){ /*parent process*/
            wait(NULL);
            int value = 10;    
            /*printf("\nPARENT: value = %d \n\n", value); /*LINE A*/    
            int i;        
            for(i=0; i<value+1; i++){
                printf("\nParent %d", i+1);
                printf("= %d",*shared_memory[i]);
            }    
            return 0;
        }    
    
    
        /*segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
        shared_memory = (int *) shmat(segment_id, NULL, 0);
        /*sprintf(shared_memory, "Hi there!");
        printf("*%s\n", shared_memory);*/
    
        shmdt(shared_memory);
        /*shmct1(segment_id, IPC_RMID, NULL);*/
        return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Looks like your fibArray is going out of bounds
    fibArray[j] where j=10

    The maximum value allowed would be
    fibArray[j] where j=9
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > int *shared_memory[10];
    Try to explain what this is trying to do.

    You don't even initialise it before doing this
    *shared_memory[0]=1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL in Shared Memory
    By AlenDev in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2010, 04:04 AM
  2. Replies: 1
    Last Post: 03-26-2010, 12:50 AM
  3. Array of srtuct and shared memory
    By mr.toc in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 06:45 AM
  4. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  5. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21