Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3082

Re: int vs. unsigned int vs. size_t

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: int vs. unsigned int vs. size_t
Date 2011-05-01 10:37 +1200
Message-ID <923h9fFj03U3@mid.individual.net> (permalink)
References <ipgasf$1q5b$1@adenine.netfront.net> <iphh5a$o71$1@nnrp.ngi.it> <923bp1Fj03U1@mid.individual.net> <923difFu7jU1@mid.individual.net>

Show all headers | View raw


On 05/ 1/11 09:34 AM, Tim Prince wrote:
> On 4/30/2011 2:03 PM, Ian Collins wrote:
>> On 05/ 1/11 05:33 AM, pozz wrote:
>>> Il 30/04/2011 08:40, Steve Keller ha scritto:
>>>> #define ASIZE(a) (sizeof(a) / sizeof((a)[0])
>>>> int a[] = { ... };
>>>>
>>>> void foo(void)
>>>> {
>>>> int i;
>>>> for (i = 0; i<  ASIZE(a); i++)
>>>> /* do something with a[i]. */
>>>> }
>>>
>>> I'm reading "C Unleashed" book and I found in some listings the
>>> following code:
>>>
>>> #define ASIZE(a) (sizeof(a) / sizeof((a)[0])
>>> int a[] = { ... };
>>>
>>> void foo(void)
>>> {
>>> int i;
>>> for (i = 0; i<  (int) ASIZE(a); i++)
>>> /* do something with a[i]. */
>>> }
>>
>> That's pretty gross. If they are going to use an invariant of one type,
>> they should use a counter of the same type and not an ugly, pointless
>> and distracting cast.
>>
> The code evidently supported only INT_MAX sized arrays.  If there were
> any possibility of larger than that (certainly not far fetched
> nowadays), this ASIZE macro could have been asserted<= INT_MAX ahead of
> the loop.  size_t is not necessarily a good choice for a loop counter;
> the largest signed int type no larger than size_t might be useful.

The range of the loop is 0 to the array length, which is a size_t.  So 
size_t is the natural choice.

The largest signed int type no larger than size_t will vary, so yet more 
pointless code would be required to determine what it is...

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

int vs. unsigned int vs. size_t Steve Keller <keller@no.invalid> - 2011-04-30 08:40 +0200
  Re: int vs. unsigned int vs. size_t Tim Prince <tprince@computer.org> - 2011-04-30 09:27 -0700
    Re: int vs. unsigned int vs. size_t Tim Prince <tprince@computer.org> - 2011-04-30 12:55 -0700
    Re: int vs. unsigned int vs. size_t Ian Collins <ian-news@hotmail.com> - 2011-05-01 10:34 +1200
  Re: int vs. unsigned int vs. size_t pozz <pozzugno@gmail.com> - 2011-04-30 19:33 +0200
    Re: int vs. unsigned int vs. size_t Ian Collins <ian-news@hotmail.com> - 2011-05-01 09:03 +1200
      Re: int vs. unsigned int vs. size_t Tim Prince <tprince@computer.org> - 2011-04-30 14:34 -0700
        Re: int vs. unsigned int vs. size_t Ian Collins <ian-news@hotmail.com> - 2011-05-01 10:37 +1200
  Re: int vs. unsigned int vs. size_t Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-04-30 19:41 -0400
    Re: int vs. unsigned int vs. size_t Keith Thompson <kst-u@mib.org> - 2011-04-30 17:46 -0700
      Re: int vs. unsigned int vs. size_t Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-01 21:27 -0400
  Re: int vs. unsigned int vs. size_t Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-04-30 22:23 -0700
  Re: int vs. unsigned int vs. size_t Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-01 14:16 +0100
    Re: int vs. unsigned int vs. size_t Barry Schwarz <schwarzb@dqel.com> - 2011-05-01 10:31 -0700
    Re: int vs. unsigned int vs. size_t Keith Thompson <kst-u@mib.org> - 2011-05-01 12:25 -0700
      Re: int vs. unsigned int vs. size_t James Kuyper <jameskuyper@verizon.net> - 2011-05-01 20:00 -0400
        Re: int vs. unsigned int vs. size_t Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-02 10:17 -0700
      Re: int vs. unsigned int vs. size_t Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-02 08:37 +0100
  Re: int vs. unsigned int vs. size_t ImpalerCore <jadill33@gmail.com> - 2011-05-03 06:02 -0700
    Re: int vs. unsigned int vs. size_t Peter Nilsson <airia@acay.com.au> - 2011-05-03 15:55 -0700
    Re: int vs. unsigned int vs. size_t Luca Forlizzi <luca.forlizzi@gmail.com> - 2011-05-05 02:38 -0700
      Re: int vs. unsigned int vs. size_t ImpalerCore <jadill33@gmail.com> - 2011-05-05 06:37 -0700
        Re: int vs. unsigned int vs. size_t Angel <angel+news@spamcop.net> - 2011-05-05 16:10 +0000
        Re: int vs. unsigned int vs. size_t Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-05 09:35 -0700
          Re: int vs. unsigned int vs. size_t Keith Thompson <kst-u@mib.org> - 2011-05-05 11:22 -0700
            Re: int vs. unsigned int vs. size_t Peter Nilsson <airia@acay.com.au> - 2011-05-05 16:06 -0700
            Re: int vs. unsigned int vs. size_t Michael Press <rubrum@pacbell.net> - 2011-05-06 02:32 -0700
        Re: int vs. unsigned int vs. size_t Luca Forlizzi <luca.forlizzi@gmail.com> - 2011-05-07 02:42 -0700

csiph-web