2 .\" Copyright (C) 2022 Jan Schaumann <jschauma@netmeister.org>.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .Nd yield the storage size of the given operand
39 operator yields the size of its operand.
42 operator cannot be applied to incomplete types and expressions
43 with incomplete types (e.g.
49 The size of primitive (non-derived) data types in C may differ
50 across hardware platforms and implementations.
51 They are defined by corresponding Application Binary Interface (ABI)
54 for details about ABI used by
56 It may be necessary or useful for a program to be able
57 to determine the storage size of a data type or object
58 to account for the platform specifics.
62 operator yields the storage size of an expression or
68 is always guaranteed to be 1.
69 (The number of bits per
75 header; many systems also provide the "number of bits
76 per byte" definition as
82 Different platforms may use different data models.
83 For example, systems on which integers, longs, and
84 pointers are using 32 bits (e.g., i386) are referred
85 to as using the "ILP32" data model, systems using
86 64 bit longs and pointers (e.g., amd64 / x86_64)
87 as the "LP64" data model.
89 The following examples illustrate the possible results
92 on an ILP32 vs. an LP64 system:
94 When applied to a simple variable or data type,
96 returns the storage size of the data type of the object:
97 .Bl -column -offset indent \
98 ".Li sizeof(struct flex)" ".Sy Result (ILP32)" ".Sy Result (LP64)"
99 .It Sy Object or type \
100 Ta Sy Result (ILP32) \
102 .It Li sizeof(char) \
108 .It Li sizeof(long) \
111 .It Li sizeof(float) \
114 .It Li sizeof(double) \
117 .It Li sizeof(char *) \
122 For initialized data or uninitialized arrays of a
123 fixed size known at compile time,
125 will return the correct storage size:
126 .Bd -literal -offset indent
127 #define DATA "1234567890"
130 char buf3[1024] = { 'a', 'b', 'c' };
132 .Bl -column -offset indent \
133 ".Li sizeof(struct flex)" ".Sy Result"
134 .It Sy Object or type \
136 .It Li sizeof(DATA) \
138 .It Li sizeof(buf1) \
140 .It Li sizeof(buf2) \
142 .It Li sizeof(buf3) \
146 The examples above are the same for ILP32 and LP64
147 platforms, as they are based on character units.
149 When applied to a struct or union,
151 returns the total number of bytes in the object,
152 including any internal or trailing padding used to
153 align the object in memory.
154 This result may thus be larger than if the storage
155 size of each individual member had been added:
156 .Bd -literal -offset indent
184 .Bl -column -offset indent \
185 ".Li sizeof(struct flex)" ".Sy Result (ILP32) " ".Sy Result (LP64)"
186 .It Sy Object or type \
187 Ta Sy Result (ILP32) \
189 .It Li sizeof(struct s1) \
192 .It Li sizeof(struct s2) \
195 .It Li sizeof(struct s3) \
198 .It Li sizeof(struct s4) \
201 .It Li sizeof(struct s5) \
206 When applied to a struct containing a flexible array
209 returns the size of the struct
211 the array, although again possibly including any
212 padding the compiler deemed appropriate:
213 .Bd -literal -offset indent
220 .Bl -column -offset indent \
221 ".Li sizeof(struct flex)" ".Sy Result (ILP32) " ".Sy Result (LP64)"
222 .It Sy Object or type \
223 Ta Sy Result (ILP32) \
225 .It Li sizeof(struct flex) \
230 One of the more common uses of the
232 operator is to determine the correct amount of memory
234 .Bd -literal -offset indent
235 int *nums = calloc(512, sizeof(int));
240 operator can be used to calculate the number of
241 elements in an array by dividing the size of the array
242 by the size of one of its elements:
243 .Bd -literal -offset indent
244 int nums[] = { 1, 2, 3, 4, 5 };
245 const int howmany = sizeof(nums) / sizeof(nums[0]);
248 Many systems provide this shortcut as the macro
256 operator is an unsigned integer type, defined in the
261 It is a common mistake to apply
263 to a dynamically allocated array:
264 .Bd -literal -offset indent
266 if ((buf = malloc(BUFSIZ)) == NULL) {
269 /* Warning: wrong! */
270 (void)strncat(buf, input, sizeof(buf) - 1);
273 In that case, the operator will return the storage
274 size of the pointer (
282 of the result of the expression given, but
284 evaluate the expression:
285 .Bd -literal -offset indent
287 printf("%ld - %d\\n", sizeof(a = 10), a); /* Result: "4 - 42" */
290 Since it is evaluated by the compiler and not the
293 operator cannot be used in a preprocessor expression.
303 Handling of flexible array members in structures
307 This manual page was written by
308 .An Jan Schaumann Aq Mt jschauma@netmeister.org .