【问题标题】:Why am I getting a segmentation-fault?为什么我会收到分段错误?
【发布时间】:2013-11-22 19:10:03
【问题描述】:

这是我的代码:

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
}

【问题讨论】:

  • 你运行它的调试器说....?

标签: c unix memory segmentation-fault shared


【解决方案1】:

你需要打电话给shmget,而不是semget

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多