sizeof(char[]) in a struct member with no size specified

94 Views Asked by At
#include <iostream>

struct inotify_event {
  int wd;
  uint32_t mask;
  uint32_t cookie;
  uint32_t len;
  char name[];
};

struct inotify_event_no_name {
  int wd;
  uint32_t mask;
  uint32_t cookie;
  uint32_t len;
};

int main() {
  std::cout << "sizeof(int): " << sizeof(int) << std::endl;
  std::cout << "sizeof(inotify_event): " << sizeof(inotify_event) << std::endl;
  std::cout << "sizeof(inotify_event_no_name): " << sizeof(inotify_event_no_name) << std::endl;

  return 0;
}

Output:

sizeof(int): 4
sizeof(inotify_event): 16
sizeof(inotify_event_no_name): 16

The len field counts all of the bytes in name, including the null bytes; the length of each inotify_event structure is thus sizeof(struct inotify_event)+len.

Why does inotify_event have the same size as inotify_event_no_name?

0

There are 0 best solutions below