Search code examples
cmanpage

Does the definition "needs to be at least XX bytes long" include the null terminator, or should I use XX+1?


I was wondering, when you see an API that says that a character buffer should be at least XXX byte long, does it include the null terminating character? Not that I mind sparing a byte, but this thing bugs me... (no double meaning here!)

From the Mac OS X manpage for if_indextoname:

The if_indextoname() function maps the interface index specified in ifindex to it corresponding name, which is copied into the buffer pointed to by ifname, which must be of at least IFNAMSIZ bytes.


Solution

  • The character buffer is a NUL terminated string. Therefore the buffer needs to be large enough to hold the string, including the NUL.