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


Groups > comp.lang.c > #3122

Re: int vs. unsigned int vs. size_t

From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: int vs. unsigned int vs. size_t
Date 2011-05-02 08:37 +0100
Message-ID <87mxj5v9gr.fsf@temporary-address.org.uk> (permalink)
References <ipgasf$1q5b$1@adenine.netfront.net> <87tydev9vx.fsf@temporary-address.org.uk> <lntydeuss8.fsf@nuthaus.mib.org>

Show all headers | View raw


Keith Thompson <kst-u@mib.org> writes:

> Dr Nick <3-nospam@temporary-address.org.uk> writes:
>> Steve Keller <keller@no.invalid> writes:
>>> This is a style question.  To iterate through an array I often use
>>> code similar to the following:
>>>
>>> 	#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]. */
>>> 	}
>>>
>>> With all warnings enabled, gcc will warn about a comparison between
>>> signed and unsigned integer types, because sizeof() yields a size_t
>>> which is unsigned.  To eliminate that warning I could use unsigned int
>>> or size_t for the variable i.  But IMO int feels most natural and
>>> size_t might have unwanted overhead, if it is a large integer type and
>>> I know that the array size will be very small (i.e. less than INT_MAX
>>> elements).
>>>
>>> So, what type for i do you prefer in cases like this?
>>
>> I hit another issue with this a couple of days ago.  If you /do/ use an
>> unsigned type for 'i', and the array has no items in it, and you use GCC
>> with -W you get:
>> warning: comparison is always false due to limited range of data type
>
> How could the array have no items in it?  C doesn't have zero-sized
> arrays.

I did wonder about that, I presume it's (another) GNU extension.

I didn't intend the array to have no elements in it.  It was being used
like this:

extern void Populate_Autoconf_Variable(value *val) {
  unsigned int i;
  char* wk[][2] = {
#include "processed_config_h"
  };
  for(i=0; i<sizeof wk/sizeof wk[1]); i++) {
    Set_Lookup_String(val,wk[i][0],wk[i][1]);
  }
}

where processed_config_h should be a list of items like this:
{"HAVE_FLOCK","1"},
{"HAVE_GD","1"},
{"HAVE_GNUPLOT","1"},
{"HAVE_INTTYPES_H","1"},
{"HAVE_LIBDL","1"},

so it allows the scripting language to have access to the details of the
configuration it was built under.  But the code to generate
processed_config_h was failing and creating an empty file.

The result was the previously mentioned error at the for, not anything
at the array definition.

Oh, and all praise to the C committee for allowing that final trailing
comma, it does make life a lot easier.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

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