Changeset View
Changeset View
Standalone View
Standalone View
sizeof.7
Context not available. | |||||
.br | .br | ||||
.Nm Vt expression | .Nm Vt expression | ||||
.Sh DESCRIPTION | .Sh DESCRIPTION | ||||
The | |||||
.Nm | |||||
operator yields the size of its operand. | |||||
The | |||||
.Nm | |||||
operator cannot be applied to incomplete types and expressions | |||||
with incomplete types (e.g. | |||||
.Vt void , | |||||
or forward-defined | |||||
.Vt struct foo ), | |||||
and function types. | |||||
.Pp | |||||
The size of primitive (non-derived) data types in C may differ | |||||
across hardware platforms and implementations. | |||||
They are defined by corresponding Application Binary Interface (ABI) | |||||
specifications, see | |||||
.Xr arch 7 | |||||
for details about ABI used by | |||||
.Fx . | |||||
It may be necessary or useful for a program to be able | |||||
to determine the storage size of a data type or object | |||||
to account for the platform specifics. | |||||
.Pp | |||||
The unary | The unary | ||||
.Nm | .Nm | ||||
operator yields the storage size of an expression or | operator yields the storage size of an expression or | ||||
data type in | data type in bytes. | ||||
.Em char sized units | .Sh DETAILS | ||||
(C language bytes). | The size of data types in C (such as e.g., integers or | ||||
pointers) may differ across hardware platforms and | |||||
implementations. | |||||
For example, systems on which integers, longs, and | |||||
pointers are using 32 bits (e.g., i386) are referred | |||||
to as using the "ILP32" data model, systems using | |||||
64 bit longs and pointers (e.g., amd64 / x86_64) | |||||
as the "LP64" data model. | |||||
.Pp | |||||
As it may be necessary or useful for a program to be able | |||||
to determine the storage size of a data type or | |||||
object, | |||||
.Nm | |||||
yields that size in | |||||
.Em char sized units . | |||||
As a result, | As a result, | ||||
.Ql sizeof(char) | .Pf ' Vt sizeof(char) Ns ' | ||||
is always guaranteed to be 1. | is always guaranteed to be | ||||
1. | |||||
(The number of bits per | (The number of bits per | ||||
.Vt char | .Vt char | ||||
is given by the | is given by the | ||||
Context not available. | |||||
.In sys/param.h | .In sys/param.h | ||||
header.) | header.) | ||||
.Sh EXAMPLES | .Sh EXAMPLES | ||||
Different platforms may use different data models. | |||||
For example, systems on which integers, longs, and | |||||
pointers are using 32 bits (e.g., i386) are referred | |||||
to as using the "ILP32" data model, systems using | |||||
64 bit longs and pointers (e.g., amd64 / x86_64) | |||||
as the "LP64" data model. | |||||
.Pp | |||||
The following examples illustrate the possible results | The following examples illustrate the possible results | ||||
of calling | of calling | ||||
.Nm | .Nm | ||||
Context not available. | |||||
.Pp | .Pp | ||||
When applied to a simple variable or data type, | When applied to a simple variable or data type, | ||||
.Nm | .Nm | ||||
returns the storage size of the data type of the object: | returns the storage size of the data type of the | ||||
object: | |||||
.Bl -column -offset indent \ | .Bl -column -offset indent \ | ||||
".Li sizeof(struct flex)" ".Sy Result (ILP32)" ".Sy Result (LP64)" | ".Li sizeof(struct flex)" ".Sy Result (ILP32)" ".Sy Result (LP64)" | ||||
.It Sy Object or type \ | .It Sy Object or type \ | ||||
Context not available. | |||||
.Ed | .Ed | ||||
.Pp | .Pp | ||||
In that case, the operator will return the storage | In that case, the operator will return the storage | ||||
size of the pointer ( | size of the pointer | ||||
.Ql sizeof(char *) | .Pf (' Vt sizeof(char *) Ns '), | ||||
), not the | not the | ||||
allocated memory. | allocated memory. | ||||
.Pp | .Pp | ||||
.Nm | .Nm | ||||
Context not available. | |||||
preprocessor, the | preprocessor, the | ||||
.Nm | .Nm | ||||
operator cannot be used in a preprocessor expression. | operator cannot be used in a preprocessor expression. | ||||
.Pp | |||||
The | |||||
.Nm | |||||
operator cannot be used on a bit-field object, a | |||||
function type, or an incomplete type. | |||||
.Sh SEE ALSO | .Sh SEE ALSO | ||||
.Xr arch 7 , | .Xr arch 7 , | ||||
.Xr operator 7 | .Xr operator 7 | ||||
Context not available. |