Re: Type of argc



On Mon, 10 Sep 2007 03:24:05 +0000, Richard Heathfield
<rjh@xxxxxxxxxxxxxxx> wrote in comp.std.c:

Jack Klein said:

On Sun, 09 Sep 2007 07:10:35 +0000, Richard Heathfield
<rjh@xxxxxxxxxxxxxxx> wrote in comp.std.c:


<snip>

The whole point of size_t is to record object counts
and sizes, and the role of argc is to provide a count of the number
of pointers in the array to whose first element argv points, so
size_t is absolutely the right type for it.

[snip]

Chapter and verse please. [...]

I see nothing in the standard even suggesting that size_t is a good
choice for counts.

calloc, fread, and fwrite spring to mind.

And you snipped the part of my post where I mentioned that:

"And it is accepted as a
parameter by several library functions where it represents the a
memory size in bytes, and in a few cases as an additional parameter
along with a size in bytes, which will be multiplied with the size in
bytes, to yield a total size in bytes."

Nowhere in the standard that I can think of off-hand is the type
size_t used other than directly representing a size in bytes, or as a
value passed along with another size_t that does represent a size in
bytes.

In those cases where size_t is used to pass a count to a standard
library function, I believe it is _always_ used to multiply with
another size_t argument that is a size in bytes, to produce a product
that is a size in bytes.

So again, can you cite any of the following:

-- a library function that takes a single size_t parameter that does
not represent a size in bytes, or a function taking two size_t
parameters where neither of them is a size in bytes or the two size_t
values are not multiplied together to produce a total size in bytes?

-- any other place in the standard where size_t is used to represent
anything else other than a size in bytes?

-- any place where the standard recommends use of size_t for counting
anything other than a size in bytes?


While size_t can certainly be used for counts, I don't see how it
provides any benefits over any other integer type, signed or unsigned,
of sufficient range for the values it will contain.

In particular, it has no real relationship to the use of argc, because
the user program rarely cares at all about the size of the block of
memory, in bytes, taken up by the argv pointer array. Only the total
number of them.

In other words, a count of objects. My point exactly.

If argc was the size of the argv array, in bytes, the type size_t
would be appropriate. You could then obtain the argument count by
dividing argc by sizeof *argv.

The standard mandates and includes the type size_t for one specific
purpose, namely that of holding values of sizes in bytes.

Using size_t for values representing anything other than sizes in
bytes is amateurish at best, confusing at worst. And, of course,
possibly error-prone, as code may fail when ported to a platform with
a smaller size_t that the original.

Or would you replace appropriate sized signed integer types with
ptrdiff_t?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Need help finding a supplier...
    ... The general process for design is to first establish the standard sizes ... gears, gear boxes, etc. etc. that you are likely to need. ...
    (rec.crafts.metalworking)
  • Where do all those funny standards come from
    ... We got into a discussion of all the metalworking and related standards: a mile is 1000 Roman Soldier paces, standard train track gage is from the Roman chariots. ... Letter drill sizes are just the odd sizes needed in between fractional sizes. ...
    (rec.crafts.metalworking)
  • Re: new rooflight
    ... However Fiamma etc all ... come in standard 500mm sizes. ... I thought it looked like an MPK make but ...
    (uk.rec.caravanning)
  • Re: size of pointer in C?
    ... >>> The quickest way to find out what pointer sizes are on your particular ... the C standard only uses the ... > properly whether they differ or not. ...
    (comp.lang.c)
  • Re: Sorry, int main (int argc, char **argv) again.
    ... |>| Never because argv is an array. ... | Actually the C Standard is more precise. ... If argc is greater than ... vector being a null pointer. ...
    (alt.comp.lang.learn.c-cpp)