The use of 'size_t'

Started by
0 comments, last by Hodgman 11 years, 8 months ago
When reading back through my copy of "The C Programming Language" I noticed I'd missed a line that states that that the type 'size_t' is used only used for the return of the 'sizeof' operator and is only guaranteed to be large enough to hold the largest type size.

Why is it then that this type is used consistent when talking about conceptual size (like the size of an array or a file size)? I've seen it plastered all over the Internet and no-one has ever said anything against this kind of use...
[source lang="cpp"]# include <stdio.h>

int main(int argc, char *argv[])
{
size_t fsize = /* return the size of a previously opened file */;

return 0;
}[/source]
Can anyone clarify the use of such a type?
Advertisement
is only guaranteed to be large enough to hold the largest type size
N.B. this includes arrays, so if I make an structure with an array of 10000 integers in it, and then make an array of 10000 of those structures, assuming that such an array is possible to create, then [font=courier new,courier,monospace]size_t[/font] has to be able to describe it's size in chars.

Also, in practice, [font=courier new,courier,monospace]SIZE_MAX[/font] is usually either [font=courier new,courier,monospace]UINT_MAX[/font] or [font=courier new,courier,monospace]ULLONG_MAX[/font], depending on the native register size, so it makes a good "native sized unsigned integer" type.

This topic is closed to new replies.

Advertisement