Using ssize_t vs int

后端 未结 3 797
陌清茗
陌清茗 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: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).

提交回复
热议问题