0

Im trying to build a program that parsing a 2d dynamic array to other program by using shared memory.I search a lot but im a bit confused because im not familiar at this one. My code so far:

int main (int argc, char* argv []){
    int rows,columns;
    if( argc < 3 ){
        printf("Need The size of the 2d array\n");
        return 0;
    }
    rows = atoi(argv[1]);
    columns = atoi(argv[2]);

    time_t t;
    srand((unsigned) time(&t));

    key_t key = ftok(".",'a');
    size_t size = sizeof(key_t) + (rows * columns + 2 + rows) * sizeof(int);
    int shmid = shmget(key,size,IPC_CREAT|IPC_EXCL|S_IRWXU);
    int *memory = shmat(shmid, NULL, 0);
    printf("Shared Memory Key: %d\n", key);

    int *argsflag = memory;
    int *resflag= memory + 1;
    int *res  = memory + 2;

    int **array = (int **) memory + (rows*columns);

    for(int i = 0; i < rows ; i++) {
        for(int j = 0; j < columns; j++) {
            array[i][j] = rand() % 100;
        }
    }
    for(int i = 0; i < rows ; i++) {
        for(int j = 0; j < columns; j++) {
            printf("%d ",array[i][j]);
        }
        printf("\n");
    }


    shmctl(shmid,IPC_RMID,NULL);
    shmdt(memory);
    return(0);
}

Im getting a Segmentation fault (core dumped) and i dont know why.Also by searching i find a solution with struct but i dint get how i can build that.

4
  • 2
    A pointer to a pointer is not the same as an array of arrays. See e.g. this old answer of mine for an explanation of why. To solve your problem you need to create array as a pointer to an array (e.g. int (*array)[COLUMN_SIZE];). Also, your calculation memory + (rows*columns) seems wrong, as it probably points to almost the end of the array. Commented Feb 4, 2020 at 11:51
  • For the memory i want to parse at shared memory one key and the 2d dynamic array of int Commented Feb 4, 2020 at 11:55
  • I see thet answer for the arrays and i think i know the differend!But still on my problem,the think that i dont know is how to parse my 2d dynamic array to allready allocated memory of shared memory. Commented Feb 4, 2020 at 12:01
  • As a side note, you should sanitize the argv data, make sure it is within sensible ranges etc. strlen will tell you the number of digits, which is a good indication. Once you have done that, you should always use strtoul (family) and not atoi, because the latter has no error handling and relies on prisitine input, while the former stops gracefully when it encounters bad data. Commented Feb 4, 2020 at 12:54

1 Answer 1

2

You cannot have a int** point at a 2D array. It can only point to the first element in a 1D array of int*.

Furthermore, what's the logic of memory + (rows*columns)? You end up setting the pointer to the last item of the array, rather than the first.

Try this instead:

void* memory = shmat( ... 
...
int (*array)[columns] = memory;
...
array[i][j] = ... ;

Where int (*array)[columns] is an array pointer, which ends up point at the first array in the 2D array.

For details, see Correctly allocating multi-dimensional arrays.

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

4 Comments

I change my array to int (array)[columns] = memory; as you said and then i have to allocate the array? LIke that for example array[i] = (int) malloc(columns*sizeof(int))?For 1st i make an array of pointers, now i have to make those pointers look at my int array?
@ΣωτηρηςΙωαννιδης Just forget about arrays of pointers, it's not what you have, want or need.
After the change i made im still getting the same error! int (*array)[columns] = memory; after this my array is ready to save on it integers or i have to make somethink else cause im still confused about
@ΣωτηρηςΙωαννιδης Might have something to do with your use of shmget & friends then. I don't know this API well, so I can't help you there.

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.