Jordan Davis
Jordan Davis

Reputation: 1520

Data Type - socklen_t, sa_family_t

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:

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

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

Upvotes: 7

Views: 28760

Answers (1)

dbush
dbush

Reputation: 224342

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.

Upvotes: 10

Related Questions