简体   繁体   中英

segmentation fault on shared memory

I'm having some hard time with the shared memory, I get segmentation fault when I try to see what's inside a variable

printf("|%d|", mappa->map[i][j]->taxi);

but not when I'm writing on it

mappa->map[i][j]->taxi = rand() % 10;

my first guess was: ok I'm not allocating enough space. But wouldn't fail also the instruction when trying to write on "taxi"?

PS I need to have "taxi" inside a struct because later I will have more elements in that struct

#define KEY 6666

typedef struct cell {
    int taxi;
} cell;
typedef cell *cella;

typedef struct city
{
    cella map[5][5];
} city;

typedef city *citta;


int main(int argc, char const *argv[])
{
    citta mappa;
    cella single_cell;
    int id_mappa;
    int id_cell;
    id_mappa = shmget(KEY, sizeof(*mappa), IPC_CREAT | 0666);
    mappa = shmat(id_mappa, NULL, 0);
    srand(time(NULL));
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            printf("ciao\n");
            id_cell = shmget(KEY + i + j, sizeof(*single_cell), IPC_CREAT | 0666);
            mappa->map[i][j] = shmat(id_cell, NULL, 0);
            mappa->map[i][j]->taxi = rand() % 10;
            printf("|%d|", mappa->map[i][j]->taxi);
        }
        printf("\n");
    }
    printf("-------------\n");
}

when doing:

id_cell = shmget(KEY + i + j, sizeof(*single_cell), IPC_CREAT | 0666);

you use several times the same key, in the first turn of the loop i and j are 0 so the used key is KEY + 0 + 0 being KEY already used for mappa and then on the first turn of the loop mappa->map[i][j] = shmat(id_cell, NULL, 0); is 'like' to do mappa->map[i][j] = mappa then mappa->map[i][j]->taxi = rand() % 10; does not have the expected behavior for instance setting mappa->map[i][j] with the random value which is not a valid pointer when you try to dereference it next line to print the value of mappa->map[i][j]->taxi .

Do for instance:

id_cell = shmget(KEY + i + j*10 + 1, sizeof(*single_cell), IPC_CREAT | 0666);

to have a different key each time (you can just multiply j by 5 of course, or more simple use a var you increment each time you call shmget etc)

after that :#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>
#include <stdlib.h>

#define KEY 6666

typedef struct cell {
    int taxi;
} cell;
typedef cell *cella;

typedef struct city
{
    cella map[5][5];
} city;

typedef city *citta;


int main(int argc, char const *argv[])
{
    citta mappa;
    cella single_cell;
    int id_mappa;
    int id_cell;
    id_mappa = shmget(KEY, sizeof(*mappa), IPC_CREAT | 0666);
    mappa = shmat(id_mappa, NULL, 0);
    srand(time(NULL));
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            printf("ciao\n");
            id_cell = shmget(KEY + i + j*10 + 1, sizeof(*single_cell), IPC_CREAT | 0666);
            mappa->map[i][j] = shmat(id_cell, NULL, 0);
            mappa->map[i][j]->taxi = rand() % 10;
            printf("|%d|", mappa->map[i][j]->taxi);
        }
        printf("\n");
    }
    printf("-------------\n");
}

Compilation and executions:

pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
ciao
|3|ciao
|5|ciao
|0|ciao
|9|ciao
|5|
ciao
|0|ciao
|7|ciao
|8|ciao
|3|ciao
|1|
ciao
|3|ciao
|2|ciao
|7|ciao
|0|ciao
|1|
ciao
|4|ciao
|4|ciao
|5|ciao
|2|ciao
|0|
ciao
|8|ciao
|2|ciao
|6|ciao
|4|ciao
|1|
-------------
pi@raspberrypi:/tmp $ 

and

pi@raspberrypi:/tmp $ valgrind ./a.out
==8741== Memcheck, a memory error detector
==8741== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8741== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==8741== Command: ./a.out
==8741== 
ciao
|3|ciao
|2|ciao
|7|ciao
|6|ciao
|3|
ciao
|5|ciao
|8|ciao
|1|ciao
|9|ciao
|2|
ciao
|3|ciao
|7|ciao
|9|ciao
|0|ciao
|3|
ciao
|9|ciao
|1|ciao
|3|ciao
|9|ciao
|5|
ciao
|8|ciao
|6|ciao
|1|ciao
|3|ciao
|1|
-------------
==8741== 
==8741== HEAP SUMMARY:
==8741==     in use at exit: 0 bytes in 0 blocks
==8741==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==8741== 
==8741== All heap blocks were freed -- no leaks are possible
==8741== 
==8741== For lists of detected and suppressed errors, rerun with: -s
==8741== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $ 

Out of that to hide the pointers through typedef (example typedef cell *cella; ) is a very bad idea producing a lot of problem and making your code less readable, I strongly encourage you to let the * visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM