Re: Using size_t clearly (appropriately?)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 29 Jun 2006 00:18:20 GMT
Andrey Tarasevich <andreytarasevich@xxxxxxxxxxx> writes:
Mark Odell wrote:
I've always declared variables used as indexes into arrays to be of
type 'size_t'. I have had it brought to my attention, recently, that
size_t is used to indicate "a count of bytes" and that using it
otherwise is confusing.
It might me. Not as much "confusing", as conceptually
incorrect. 'size_t' type is intended to be used to represent a
concept of 'size of an object'. Number of elements in the array is
described by a completely different concept of 'number of elements
in a container'. Note, that is case of generic container these two
concepts are completely unrelated. In the particular case of an
_array_ there's certain "parasitic" relationship between the two:
the latter cannot be greater than the former. This is often used as
a justification for using 'size_t' to represent array indices. This
is a false reasoning. In general case, once again, using 'size_t'
for this purpose is a conceptual error.
That's well argued, but I disagree.
We use what we have. We have a type size_t that's designed to count
sizes (in bytes) of objects. We don't have a similar type that's
designed to count the number of elements in an array of struct foobar.
If we had such a type, I'd advocate using it (for example, if
declaring "struct foobar" implicitly created an unsigned int typedef
called, say, "struct_foobar_count").
Using size_t to count objects isn't ideal, but it's what we have.
Since objects (other than bit fields, which we generally wouldn't be
interested in counting) are at least one byte each, we know that
size_t has *at least* enough range for the purpose. I don't believe
any other type would be any better, and size_t isn't sufficiently bad
that I'd recommend avoiding it.
If the language had a type to be used generically for counting
objects, surely it would be just an alias for size_t, since the
objects could be bytes in an array. I'm not greatly distressed by the
fact that it's called "size_t" rather than "object_count_t".
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: Using size_t clearly (appropriately?)
- From: Andrey Tarasevich
- Re: Using size_t clearly (appropriately?)
- References:
- Using size_t clearly (appropriately?)
- From: Mark Odell
- Re: Using size_t clearly (appropriately?)
- From: Andrey Tarasevich
- Using size_t clearly (appropriately?)
- Prev by Date: Re: Using size_t clearly (appropriately?)
- Next by Date: Re: Nested Variable Argument Macros
- Previous by thread: Re: Using size_t clearly (appropriately?)
- Next by thread: Re: Using size_t clearly (appropriately?)
- Index(es):
Relevant Pages
|