flexible array member

Posted 2024-01-12 14:59:41 ‐ 2 min read

c

Introduction

Rencently, I raise one crash on coding about IPC (inter-process communication).

The scenario is we have one struct which need to place on shared memory, and the struct size tha I want it to be determined on the runtime.

struct FAM {
  size_t len;
  int mem[];
}

FAM* create(const char* key, int len) {
  int fd = open(key, O_RDWR | O_CREAT, 0666);
  int size = sizeof(FAM) + sizeof(int) * len;
  char *strs = (char*)malloc(size);
  int a = write(fd, strs, size);
  assert(a == size);
  free(strs);
  auto* fam = (FAM*)mmap(nullptr, size, PORT_READ | PORT_WRITE, MAP_SHARED, fd, 0);
  fam->len = len;
  close(fd);
  return fam;
}

FAM* connect(const char* key, int len) {
  int fd = open(key, O_RDWR, 0666);
  FAM* fam;
  while(fd < 0) {
    printf("waiting for memory created...");
    fd = open(key, O_RDWR, 0666);
    sleep(1);
  }
  fam = (FAM*)mmap(nullptr, sizeof(FAM) + sizeof(int) * len, PORT_READ | PORT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  assert(fam != MAP_FAILED);
  return fam;
}

int release(FAM* fam, int len) {
  munmap(fam, sizeof(FAM) + sizeof(int) * len);
  return 0;
}

support

GCC and Clang extention of C99 and C++.

reference