is size_t always unsigned?

后端 未结 6 1852
陌清茗
陌清茗 2020-12-01 07:07

As title: is size_t always unsigned, i.e. for size_t x, is x always >= 0 ?

6条回答
  •  一生所求
    2020-12-01 07:48

    According to the standard it is unsigned, however I recall that some older implementations used a signed type for the typedef.

    From an older GCC doc:

    There is a potential problem with the size_t type and versions of GCC prior to release 2.4. ANSI C requires that size_t always be an unsigned type. For compatibility with existing systems' header files, GCC defines size_t in stddef.h to be whatever type the system's sys/types.h defines it to be. Most Unix systems that define size_t in sys/types.h, define it to be a signed type. Some code in the library depends on size_t being an unsigned type, and will not work correctly if it is signed

    I'm not sure how important it would be to guard against that. My code assumes it's unsigned.

提交回复
热议问题