size_t
– size type – is a non-negative integer type whose width depends on OS support and is not less than 16-bit
. size_t
is defined in the libraries:
<stddef.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
<uchar.h>
<wchar.h>
In standard C
code, the size_t
type is often used for index
variables to access arrays and counter
variables that count for loops instead of the unsigned int
integer type. The reason is because the design of unsigned int
has a fixed width of 32-bit
and can cause overflow if used to count tuples or large loops in 64-bit
programs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">include</span> <span class="token string"><stdio.h></span></span> <span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">include</span> <span class="token string"><stdint.h></span></span> <span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">include</span> <span class="token string"><limits.h></span></span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> <span class="token keyword">int</span> $argc <span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span> $argv <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">size_t</span> $size <span class="token operator">=</span> SIZE_MAX <span class="token punctuation">;</span> <span class="token comment">// stdint.h</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> $uint <span class="token operator">=</span> UINT_MAX <span class="token punctuation">;</span> <span class="token comment">// limits.h</span> <span class="token function">printf</span> <span class="token punctuation">(</span> <span class="token string">"Max value of size_t : %zu n"</span> <span class="token punctuation">,</span> $size <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token function">printf</span> <span class="token punctuation">(</span> <span class="token string">"Max value of unsigned int : %u n"</span> <span class="token punctuation">,</span> $uint <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
1 2 3 4 | # ubuntu 64-bit Max value of size_t : 18446744073709551615 Max value of unsigned int : 4294967295 |
In addition,
size_t
is also used in places to type return values and parameters of standard library functions such as sizeof
, offsetof
, bsearch
, qsort
, etc..1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void* bsearch ( const void* key, const void* ptr, size_t count, size_t size, int (*comp) (const void*, const void*) ); void qsort ( void* ptr, size_t count, size_t size, int (*comp) (const void*, const void*) ); |