2013-11-22 56 views
-1

这是我的代码:为什么我会遇到分段错误?

struct bacchetta { 
    bool in_uso; 
}; 

int main() 
{ 
    key_t key; 
    if ((key = ftok(".", 'a')) == -1) { 
    perror("ftok"); 
    exit(1); 
    } 

    int shmid; 
    if ((shmid = semget(key, sizeof(struct bacchetta)*5, 0600 | IPC_CREAT)) == -1) { 
    perror("Errore creazione dell'area di memoria per le bacchette"); 
    exit(1); 
    } 

    bacchetta *bacchette = (bacchetta*)shmat(shmid, NULL, 0); 
    if (bacchette == NULL) { 
    perror("Errore nell'attachment dell'area di memoria"); 
    exit(2); 
    } 

    //!!!HERE segmentation-fault (core dumped) 
    if (!bacchette[0].in_uso) printf("ok");// = false; 

    return 0 
} 
+4

你正在运行它的调试器说....? – WhozCraig

回答

3

你需要调用shmget,不semget

+0

哈哈。正确。谢谢 –

+3

并且shmat在错误时返回-1。 – Duck

相关问题