[英]segmentation fault on shared memory
我在使用共享 memory 时遇到了一些困难,当我尝试查看变量内部的内容时出现分段错误
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("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");
}
做的时候:
id_cell = shmget(KEY + i + j, sizeof(*single_cell), IPC_CREAT | 0666);
您多次使用相同的键,在循环的第一轮i和j是 0 所以使用的键是KEY + 0 + 0
是已经用于mappa的KEY
然后在循环的第一轮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]->taxi
的值时,使用不是有效指针的随机值设置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("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");
}
编译和执行:
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;
)是一个非常糟糕的主意,会产生很多问题并降低代码的可读性,我强烈建议您让*
可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.