liveBook · Manning

SIZE_MAX

As explained in Modern C

SIZE_MAX is a C standard library macro that expands to the maximum value representable by size_t.

SIZE_MAX is the upper bound of the values representable by size_t. Because size_t is unsigned, its range begins at 0 and ends at SIZE_MAX. section 4.0

A platform-independent limit

Include stdint.h and use SIZE_MAX rather than hard-coding a numerical limit. Depending on the platform, the bound may be 65535, 4294967295, or 18446744073709551615. section 4.0

Keep these relationships in view:

  • Treat values from 0 through SIZE_MAX as representable size_t values. section 4.0
  • Recognize that arithmetic exceeding SIZE_MAX produces overflow, even though unsigned arithmetic remains well defined. section 4.1.1
Table 4.1. Value operators The Form column gives the syntactic form of the operation, where @ represents the operator and a and possibly b denote values that serve as operands. For arithmetic and bit operations, the type of the result is a type that reconciles the types of a and b. For some of the operators, the Nick column gives an alternative form of the operator, or lists a combination of operators that has special meaning. Most of the operators and terms will be discussed later. (view table figure)
     

type restriction

 

Operator

Nick

Form

a

b

Result

 
    a Narrow   Wide Promotion
+ -   a@b Pointer Integer Pointer Arithmetic
+ - * /   a@b Arithmetic Arithmetic Arithmetic Arithmetic
+ -   @a Arithmetic   Arithmetic Arithmetic
%   a@b Integer Integer Integer Arithmetic
~ compl @a Integer   Integer Bit
& bitand a@b Integer Integer Integer Bit
| bitor          
^ xor          
<< >>   a@b Integer Positive Integer Bit
== < > <= >=   a@b Scalar Scalar 0,1 Comparison
!= not_eq a@b Scalar Scalar 0,1 Comparison
  !!a a Scalar   0,1 Logic
!a not @a Scalar   0,1 Logic
&& || and or a@b Scalar Scalar 0,1 Logic
.   a@m struct   Value Member
*   @a Pointer   Object Reference
[]   a[b] Pointer Integer Object Member
->   a@m struct Pointer   Object Member
()   a(b ...) Function pointer   Value Call
sizeof   @ a None   size_t Size, ICE
_Alignof alignof @(a) None   size_t Alignment, ICE

Use the value-operator table to place SIZE_MAX in expressions involving arithmetic, comparison, and other operators. section 4.0

How Modern C covers it

How deep it goes
Defines the size_t range and lists common platform bounds, but defers detailed overflow behavior.
Examples
Uses 16-bit, 32-bit, and 64-bit maximum values to illustrate platform-dependent SIZE_MAX bounds.
What it assumes
Assumes familiarity with size_t and develops its nonnegative range and upper bound explicitly.
What you can do after
Evaluate size_t limits and select SIZE_MAX instead of specializing code for a particular platform.
How it connects
Connects size_t with unsigned integer types, arithmetic expressions, stdint.h, and arithmetic overflow.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.