Sign in to follow this  
Alex Melbourne

The use of 'size_t'

Recommended Posts

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? Edited by BinaryPhysics

Share this post


Link to post
Share on other sites
[quote name='BinaryPhysics' timestamp='1344520296' post='4967776']is only guaranteed to be large enough to hold the largest type size[/quote]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 "[i]native sized unsigned integer[/i]" type.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this