CBFalconer said:
Jack said:
Why did you declare j as type size_t ?
Tisdale is a troll and an idiot. There is absolutely no reason
at all to prefer size_t to int in this situation.
fprintf(stdout, " %d", m[j]);
size_t is the first type that comes to my mind
when I consider which type to use for an array index.
OK, you have rebutted Jacks last assertion. Are you also cavilling
about his other assertions?
No, but I want to get off that bandwagon as soon as possible.
Also consider that a size_t MAY
require more storage space and processing than an int.
int is not my first choice to default to
for selecting the type of an array index.
I prefer size_t and my second choice is unsigned.
I know that size_t has the range to index into any array.
I also consider that array index values
are likely to be derived from or compared to
expressions like (sizeof array / sizeof *array)
or expressions involving strlen, which have size_t types.
As far as any speed concerns go, I think that this
particular premature optimization of assuming that size_t
is significantly slower than int, is very premature.
Not worth consider without knowing the performance requirments
and the implementation.
My philosphy on saving memory by using small types
is that it mostly only makes sense for large arrays of small types.
Otherwise a situation, where the difference
between declaring a variable of type long
and declaring a variable of type int,
makes a real difference in the amount of available memory,
would be a very special situation indeed.
In such a situation, I think it would be worth investigating
whether further memory could be saved by using a char type instead.
If I had a special reason to use something instead of size_t
for an array index, then I would. For example, if I wanted
to print out the index values in C89 without using a cast.
But then, I would would still prefer unsigned over int,
to avoid any signed/unsigned mismatch problems from comparison and
assignment operations with sizeof and strlen expressions.