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.

webprogrammer
  • 2,393
  • 3
  • 21
  • 27
  • 2
    A pointer to a pointer is not the same as an array of arrays. See e.g. [this old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) 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. – Some programmer dude Feb 04 '20 at 11:51
  • For the memory i want to parse at shared memory one key and the 2d dynamic array of int – Σωτηρης Ιωαννιδης Feb 04 '20 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. – Σωτηρης Ιωαννιδης Feb 04 '20 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. – Lundin Feb 04 '20 at 12:54

1 Answers1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 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? – Σωτηρης Ιωαννιδης Feb 04 '20 at 13:08
  • @ΣωτηρηςΙωαννιδης Just forget about arrays of pointers, it's not what you have, want or need. – Lundin Feb 04 '20 at 13:54
  • 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 – Σωτηρης Ιωαννιδης Feb 04 '20 at 14:26
  • 1
    @ΣωτηρηςΙωαννιδης 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. – Lundin Feb 04 '20 at 15:01