Changeset View
Changeset View
Standalone View
Standalone View
share/man/man7/sizeof.7
Show All 28 Lines | |||||
.Nm sizeof | .Nm sizeof | ||||
operator | operator | ||||
.Nd yield the storage size of the given operand | .Nd yield the storage size of the given operand | ||||
.Sh SYNTAX | .Sh SYNTAX | ||||
.Nm Vt ( type ) | .Nm Vt ( type ) | ||||
.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) | .Ql sizeof(char) | ||||
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 | ||||
.Dv CHAR_BIT | .Dv CHAR_BIT | ||||
definition in the | definition in the | ||||
.In limits.h | .In limits.h | ||||
header; many systems also provide the "number of bits | header; many systems also provide the "number of bits | ||||
per byte" definition as | per byte" definition as | ||||
.Dv NBBY | .Dv NBBY | ||||
in the | in the | ||||
.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 | ||||
on an ILP32 vs. an LP64 system: | on an ILP32 vs. an LP64 system: | ||||
.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: | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | |||||
const int howmany = sizeof(nums) / sizeof(nums[0]); | const int howmany = sizeof(nums) / sizeof(nums[0]); | ||||
.Ed | .Ed | ||||
.Pp | .Pp | ||||
Many systems provide this shortcut as the macro | Many systems provide this shortcut as the macro | ||||
.Dv ntimes() | .Dv ntimes() | ||||
via the | via the | ||||
.In sys/param.h | .In sys/param.h | ||||
header file. | header file. | ||||
.Sh RESULT | .Sh RESULT | ||||
kib: Why changing .In to .Dv ? | |||||
The result of the | The result of the | ||||
.Nm | .Nm | ||||
operator is an unsigned integer type, defined in the | operator is an unsigned integer type, defined in the | ||||
.Dv stddef.h | .Dv stddef.h | ||||
header as a | header as a | ||||
.Vt size_t . | .Vt size_t . | ||||
.Sh NOTES | .Sh NOTES | ||||
It is a common mistake to apply | It is a common mistake to apply | ||||
.Nm | .Nm | ||||
to a dynamically allocated array: | to a dynamically allocated array: | ||||
.Bd -literal -offset indent | .Bd -literal -offset indent | ||||
char *buf; | char *buf; | ||||
if ((buf = malloc(BUFSIZ)) == NULL) { | if ((buf = malloc(BUFSIZ)) == NULL) { | ||||
perror("malloc"); | perror("malloc"); | ||||
} | } | ||||
/* Warning: wrong! */ | /* Warning: wrong! */ | ||||
(void)strncat(buf, input, sizeof(buf) - 1); | (void)strncat(buf, input, sizeof(buf) - 1); | ||||
.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 *) | .Ql sizeof(char *) | ||||
), not the | ), not the | ||||
allocated memory. | allocated memory. | ||||
Not Done Inline ActionsThis is not the the diff against current sources. kib: This is not the the diff against current sources. | |||||
.Pp | .Pp | ||||
.Nm | .Nm | ||||
determines the | determines the | ||||
.Ev size | .Ev size | ||||
of the result of the expression given, but | of the result of the expression given, but | ||||
.Em does not | .Em does not | ||||
evaluate the expression: | evaluate the expression: | ||||
.Bd -literal -offset indent | .Bd -literal -offset indent | ||||
int a = 42; | int a = 42; | ||||
printf("%ld - %d\\n", sizeof(a = 10), a); /* Result: "4 - 42" */ | printf("%ld - %d\\n", sizeof(a = 10), a); /* Result: "4 - 42" */ | ||||
.Ed | .Ed | ||||
.Pp | .Pp | ||||
Since it is evaluated by the compiler and not the | Since it is evaluated by the compiler and not the | ||||
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 | ||||
.Sh STANDARDS | .Sh STANDARDS | ||||
The | The | ||||
.Nm | .Nm | ||||
operator conforms to | operator conforms to | ||||
.St -ansiC . | .St -ansiC . | ||||
.Pp | .Pp | ||||
Handling of flexible array members in structures | Handling of flexible array members in structures | ||||
conforms to | conforms to | ||||
.St -isoC-99 . | .St -isoC-99 . | ||||
.Sh AUTHORS | .Sh AUTHORS | ||||
This manual page was written by | This manual page was written by | ||||
.An Jan Schaumann Aq Mt jschauma@netmeister.org . | .An Jan Schaumann Aq Mt jschauma@netmeister.org . |
Why changing .In to .Dv ?