Data Type - socklen_t, sa_family_t

2019-02-24 09:50发布

I'm building a simple socket web server using the sys/socket.h lib, and I came across the socklen_t and sa_family_t data types and am a bit confused on what their actual purpose is.

Definition:

  • sa_family_t - unsigned integral type.
  • socklen_t - an unsigned opaque integral type of length of at least 32-bits.

Now I understand that the <sys/socket> lib declares three structures (sockaddr,msghdr,cmsghdr) which contain members that declare these data types.

  • sa_family_t sa_family address family
  • socklen_t msg_namelen size of address
  • socklen_t msg_controllen ancillary data buffer len
  • socklen_t cmsg_len data byte count, including the cmsghdr

But why create new data types, why not just use an unsigned int data type?

1条回答
姐就是有狂的资本
2楼-- · 2019-02-24 10:25

By declaring specific types for these fields, it decouples them from a particular representation like unsigned int.

Different architectures can be free to define different sizes for these fields, and code that uses these specific types doesn't need to worry about how big an int is on a given machine.

查看更多
登录 后发表回答