Cast ssize_t or size_t

前端 未结 2 1830
故里飘歌
故里飘歌 2021-02-14 01:15

In source files which I am using in my project, there is a comparison between ssize_t and size_t variables:

ssize_t sst;
size_t st;

if         


        
2条回答
  •  粉色の甜心
    2021-02-14 01:46

    Either will work fine as long as both values fit in the positive representable range of ssize_t.

    If either value doesn't, you could end up in trouble - check those cases before testing for equality:

    if ((sst >= 0) && (st <= SSIZE_MAX) && (sst == (ssize_t)st))
    {
      ...
    }
    

    (I'm sure the C++ people will recommend you avoid the C-style cast entirely - I have no doubt someone will comment or answer and let you know the right way to do that in C++.)

提交回复
热议问题