Using ssize_t vs int

后端 未结 3 798
陌清茗
陌清茗 2020-12-08 19:22

Code

I\'ve got a function which I can write in one of four possible ways:

    int do_or_die(int retv         


        
相关标签:
3条回答
  • 2020-12-08 19:45

    You can use int or long int data types, however ssize_t is a system data type that should be used for cross-platform portability. The fundamental types (such as 'int') can be different sizes on different implementations. Usually what happens is the system type (in this case ssize_t) takes advantage of C's typedef feature so that the machine-specific data type size is used, e.g. typedef signed ssize_t (this is part of SUSv3 standard data types). It is good practice to use system data types, where possible, when implementing any kind of system-level programming.

    For a more detailed description refer to The Linux Programming Interface (Michael Kerrisk)

    0 讨论(0)
  • 2020-12-08 19:51

    Use types in a way:

    • you don't mix signed and unsigned types together and
    • you don't truncate values from larger types while storing them in smaller types (overflow/underflow)

    ssize_t might be an alias for int, yet it is not standard C and might be environment specific.

    If your program will run in specific environment, check whether sizeof(ssize_t) <= sizeof(int) and use int. Otherwise, use some other type T where sizeof(T) is greater or equal than both sizeof(int) and sizeof(ssize_t).

    0 讨论(0)
  • 2020-12-08 19:58

    There's no guarantee in the POSIX standard that sizeof(int) >= sizeof(ssize_t), nor the other way around. Typically ssize_t is larger than int, but the safe and portable option in C99 is to use intmax_t instead for the argument and the return value.

    The only guarantees you have wrt. the relationship between int and ssize_t are:

    • int can store values of at least the range [-2^15 ... 2^15-1] per ISO C
    • ssize_t can store values of at least the range [-1 ... 2^15-1] per POSIX (see _POSIX_SSIZE_MAX).

    (Interestingly, there isn't even a guarantee that ssize_t can store the negative counterparts of its positive range. It's not a signed size_t, but a "size type" with an error value.)

    0 讨论(0)
提交回复
热议问题