uint32_t vs int as a convention for everyday programming

前端 未结 4 1686
春和景丽
春和景丽 2020-12-05 10:54

When should one use the datatypes from stdint.h? Is it right to always use as a convention them? What was the purpose of the design of nonspecific size types like int and s

4条回答
  •  醉酒成梦
    2020-12-05 11:37

    My work demands that I use them and I actually love using them.

    I find it useful when I have to implement a protocol and use them inside a structure which can be a message that needs to be sent out or a holder of certain information.

    If I have to use a sequence number that needs to be incremented, I wouldn't use int because sequence numbers aren't supposed to be negative. I use uint32_t instead. I will hence know the sequence number space and can plan/code accordingly.

    The code we write will be running on 32 as well as 64 bit machine so using "int" on different bit machines results in subtle bugs which can be a pain to identify. Using unint16_t will allocate 16 bits on 32 or 64 bit architecture.

提交回复
热议问题