2
我們有幾個C應用程序,其通話使用共享內存。第一個應用程序添加消息,第二個應用程序總是從共享內存讀取數據。的valgrind - 無效的寫大小
struct messagestruct {
unsigned int sessionid;
uint8_t message[16]; //unsigned 8bit int
}__attribute__ ((__packed__));
我們需要3個會話(3個用戶)。如此定義的共享內存的大小作爲
#define SHARED_SIZE (3 * sizeof(messagestruct)) + sizeof(int)
我們複製結構對共享存儲器中訪問共享存儲器作爲
int sesskey = ftok("/path/to/a/file", "B");
int shmemoryid = shmget(sesskey, SHARED_SIZE, 0666 | IPC_CREAT);
,Valgrind的報告錯誤(無效寫入大小1)
void *shmaddr = shmat(shmemoryid, NULL, 0);
int *sessnum;
struct messagestruct *msgstruct;
sessnum = (int *)shmaddr;
msgstruct = (struct messagestruct*)((void*) shmaddr + sizeof(int));
memcpy(shmaddr, currentsessionsstruct, SHARED_SIZE); //-->valgrind error invalid write size 1
感謝任何有用的信息。
多大你試試內存塊寫? – sharptooth 2011-06-06 11:37:49
@Heandel struct messagestruct currentsessionsstruct [3]; – ramtheconqueror 2011-06-06 11:38:35
@sharptooth SHARED_SIZE是我們在應用程序之間共享的大小。一個寫入該內存塊和其他讀取。使用旗語來鎖定和解鎖內存段 – ramtheconqueror 2011-06-06 11:40:21