delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/18/15:44:22

From: buers AT gmx DOT de (Dieter Buerssner)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C++, complex, etc
Date: 18 May 2000 18:47:18 GMT
Lines: 27
Message-ID: <8g1l6o.3vs4qnf.0@buerssner-17104.user.cis.dfn.de>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000518184200 DOT 15189T-100000 AT is>
NNTP-Posting-Host: pec-90-104.tnt4.s2.uunet.de (149.225.90.104)
Mime-Version: 1.0
X-Trace: fu-berlin.de 958675638 554761 149.225.90.104 (16 [17104])
X-Posting-Agent: Hamster/1.3.13.0
User-Agent: Xnews/03.02.04
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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.

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. 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019