我在共享内存方面遇到了一些困难,当我试图查看变量内部的内容时,会出现分段错误
printf("|%d|", mappa->map[i][j]->taxi);
但不是当我在上面写的时候
mappa->map[i][j]->taxi = rand() % 10;
我的第一个猜测是:好吧,我没有分配足够的空间。但当尝试在";出租车";?
PS我需要有";出租车;在结构中,因为稍后我将在该结构中有更多元素
#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("ciaon");
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");
}
执行时
id_cell = shmget(KEY + i + j, sizeof(*single_cell), IPC_CREAT | 0666);
你多次使用同一把钥匙,在循环的第一圈中,i和j为0,因此使用的键为KEY + 0 + 0
,而KEY
已用于mappa,然后在循环的第圈中,mappa->map[i][j] = shmat(id_cell, NULL, 0);
与mappa->map[i][j] = mappa
"类似",则mappa->map[i][j]->taxi = rand() % 10;
不具有预期行为,例如,在下一行尝试取消引用mappa->map[i][j]
以打印mappa->map[i][j]->taxi
。
例如:
id_cell = shmget(KEY + i + j*10 + 1, sizeof(*single_cell), IPC_CREAT | 0666);
每次都有一个不同的密钥(当然,你可以将j乘以5,或者更简单地使用每次调用shmget时递增的var等(
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("ciaon");
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");
}
编译和执行:
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 $
和
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 $
除此之外,通过typedef隐藏指针(例如typedef cell *cella;
(是一个非常糟糕的主意,会产生很多问题并降低代码的可读性,我强烈建议您让*
可见。