[英]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.