I have a API like this,
class IoType {
......
StatusType writeBytes(......, size_t& bytesWritten);
StatusType writeObjects(......, size_t& objsWritten);
};
A senior member of the team who I respect seems to have a problem with the type size_t and suggest that I use C99 types. I know it sounds stupid but I always think c99 types like uint32_t and uint64_t look ugly. I do use them but only when it’s really necessary, for instance when I need to serialize/deserialize a structure, I do want to be specific about the sizes of my data members.
What are the arguments against using size_t? I know it’s not a real type but if I know for sure even a 32-bit integer is enough for me and a size type seems to be appropriate for number of bytes or number of objects, etc.
Use exact-size types like
uint32_twhenever you’re dealing with serialization of any sort (binary files, networking, etc.). Usesize_twhenever you’re dealing with the size of an object in memory—that’s what it’s intended for. All of the functions that deal with object sizes, likemalloc,strlen, and thesizeofoperator allsize_t.If you use
size_tcorrectly, your program will be maximally portable, and it will not waste time and memory on platforms where it doesn’t need to. On 32-bit platforms, asize_twill be 32 bits—if you instead used auint64_t, you’d waste time and space. Conversely, on 64-bit platforms, asize_twill be 64 bits—if you instead used auint32_t, your program could behave incorrectly (maybe even crash or open up a security vulnerability) if it ever had to deal with a piece of memory larger than 4 GB.