Is the max value of size_t (SIZE_MAX) defined relative to the other integer types?

后端 未结 3 505
执念已碎
执念已碎 2021-02-04 08:48

I\'m writing a library of functions that will safely convert between various numeric types or die trying. My intent is roughly equal parts create-useful-library and learn-C-edge

3条回答
  •  我寻月下人不归
    2021-02-04 08:54

    Nothing requires the maximum of size_t to be larger than int. Such architectures where SIZE_MAX is <= INT_MAX are rare though and I doubt GCC would support any of them.

    As for the fix, you can use #if:

    #if INT_MAX > SIZE_MAX
    if (u_value > SIZE_MAX) {  /* Line 10 */
        exit(EXIT_FAILURE);
    }
    #endif
    

提交回复
热议问题