Mail Archives: djgpp/2000/05/18/16:14:39
Dieter Buerssner wrote:
>
> Eli Zaretskii wrote:
>
> >On Thu, 18 May 2000, Alexei A. Frounze wrote:
> >> > In general, you don't want to assign int to size_t or vice versa.
> >> > Mixing signed and unsigned means trouble.
> >>
> >> If I'm 100% sure my "int" is positive (greater or equal 0 and less than
> >> INT_MAX), will I have problems?
> >
> >No, in that case it will work, assuming that sizeof(int) <=
> >sizeof(size_t).
>
> This test (sizeof(int) <= sizeof(size_t)) probalbly is sufficient for
> all C implementations (at least the ones know). But it is not enough
> from the C Standard. size_t could have padding bits. The correct test
> would be (after including limits.h) INT_MAX <= SIZE_MAX.
>
> So, to answer Alexei's question: When your int is <= SIZE_MAX and >= 0,
> you can savely assign it to a varible of size_t. If a varible of size_t
> (or, say the return value of strlen) is <= INT_MAX, you can savely
> assign it to a varible of type int.
Stop! There are SSIZE_MAX and _POSIX_SSIZE_MAX defined only. Are those
constants limits for size_t, right?
> To answer an other question of Alexei (from memory): "Do people
> mix up size_t and int?" I have done it, but try to avoid it now.
> Often, it will be no trouble at all. I.e. you read input with fgets
> and a buffer smaller than 2^15. Then the return of strlen is guaranteed
> to be representable as an int.
ok.
bye.
--
Alexei A. Frounze
-----------------------------------------
Homepage: http://alexfru.chat.ru
Mirror: http://members.xoom.com/alexfru
- Raw text -