diff options
Diffstat (limited to 'static/freebsd/man7/sizeof.7 3.html')
| -rw-r--r-- | static/freebsd/man7/sizeof.7 3.html | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/static/freebsd/man7/sizeof.7 3.html b/static/freebsd/man7/sizeof.7 3.html new file mode 100644 index 00000000..5083f3cb --- /dev/null +++ b/static/freebsd/man7/sizeof.7 3.html @@ -0,0 +1,304 @@ +<table class="head"> + <tr> + <td class="head-ltitle">sizeof(7)</td> + <td class="head-vol">Miscellaneous Information Manual</td> + <td class="head-rtitle">sizeof(7)</td> + </tr> +</table> +<div class="manual-text"> +<section class="Sh"> +<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1> +<p class="Pp"><code class="Nm">sizeof</code> operator — + <span class="Nd">yield the storage size of the given operand</span></p> +</section> +<section class="Sh"> +<h1 class="Sh" id="SYNTAX"><a class="permalink" href="#SYNTAX">SYNTAX</a></h1> +<p class="Pp"><code class="Nm">sizeof</code> (<var class="Vt">type</var>) + <br/> + <code class="Nm">sizeof</code> <var class="Vt">expression</var></p> +</section> +<section class="Sh"> +<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1> +<p class="Pp">The <code class="Nm">sizeof</code> operator yields the size of its + operand. The <code class="Nm">sizeof</code> operator cannot be applied to + incomplete types and expressions with incomplete types (e.g. + <var class="Vt">void</var>, or forward-defined <var class="Vt">struct foo + ),</var> and function types.</p> +<p class="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 + <a class="Xr">arch(7)</a> for details about ABI used by + <span class="Ux">FreeBSD</span>. 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.</p> +<p class="Pp" id="char">The unary <code class="Nm">sizeof</code> operator yields + the storage size of an expression or data type in + <a class="permalink" href="#char"><i class="Em">char sized units</i></a> (C + language bytes). As a result, + ‘<code class="Li">sizeof(char)</code>’ is always guaranteed to + be 1. (The number of bits per <var class="Vt">char</var> is given by the + <code class="Dv">CHAR_BIT</code> definition in the + <code class="In"><<a class="In">limits.h</a>></code> header; many + systems also provide the "number of bits per byte" definition as + <code class="Dv">NBBY</code> in the + <code class="In"><<a class="In">sys/param.h</a>></code> header.)</p> +</section> +<section class="Sh"> +<h1 class="Sh" id="EXAMPLES"><a class="permalink" href="#EXAMPLES">EXAMPLES</a></h1> +<p class="Pp">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.</p> +<p class="Pp">The following examples illustrate the possible results of calling + <code class="Nm">sizeof</code> on an ILP32 vs. an LP64 system:</p> +<p class="Pp">When applied to a simple variable or data type, + <code class="Nm">sizeof</code> returns the storage size of the data type of + the object:</p> +<table class="Bl-column Bd-indent"> + <tr id="Object"> + <td><a class="permalink" href="#Object"><b class="Sy">Object or + type</b></a></td> + <td><b class="Sy">Result (ILP32)</b></td> + <td><b class="Sy">Result (LP64)</b></td> + </tr> + <tr id="sizeof(char)"> + <td><a class="permalink" href="#sizeof(char)"><code class="Li">sizeof(char)</code></a></td> + <td>1</td> + <td>1</td> + </tr> + <tr id="sizeof(int)"> + <td><a class="permalink" href="#sizeof(int)"><code class="Li">sizeof(int)</code></a></td> + <td>4</td> + <td>4</td> + </tr> + <tr id="sizeof(long)"> + <td><a class="permalink" href="#sizeof(long)"><code class="Li">sizeof(long)</code></a></td> + <td>4</td> + <td>8</td> + </tr> + <tr id="sizeof(float)"> + <td><a class="permalink" href="#sizeof(float)"><code class="Li">sizeof(float)</code></a></td> + <td>4</td> + <td>4</td> + </tr> + <tr id="sizeof(double)"> + <td><a class="permalink" href="#sizeof(double)"><code class="Li">sizeof(double)</code></a></td> + <td>8</td> + <td>8</td> + </tr> + <tr id="sizeof(char"> + <td><a class="permalink" href="#sizeof(char"><code class="Li">sizeof(char + *)</code></a></td> + <td>4</td> + <td>8</td> + </tr> +</table> +<p class="Pp">For initialized data or uninitialized arrays of a fixed size known + at compile time, <code class="Nm">sizeof</code> will return the correct + storage size:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>#define DATA "1234567890" +char buf1[] = "abc"; +char buf2[1024]; +char buf3[1024] = { 'a', 'b', 'c' };</pre> +</div> +<table class="Bl-column Bd-indent"> + <tr id="Object~2"> + <td><a class="permalink" href="#Object~2"><b class="Sy">Object or + type</b></a></td> + <td><a class="permalink" href="#Result"><b class="Sy" id="Result">Result</b></a></td> + </tr> + <tr id="sizeof(DATA)"> + <td><a class="permalink" href="#sizeof(DATA)"><code class="Li">sizeof(DATA)</code></a></td> + <td>11</td> + </tr> + <tr id="sizeof(buf1)"> + <td><a class="permalink" href="#sizeof(buf1)"><code class="Li">sizeof(buf1)</code></a></td> + <td>4</td> + </tr> + <tr id="sizeof(buf2)"> + <td><a class="permalink" href="#sizeof(buf2)"><code class="Li">sizeof(buf2)</code></a></td> + <td>1024</td> + </tr> + <tr id="sizeof(buf3)"> + <td><a class="permalink" href="#sizeof(buf3)"><code class="Li">sizeof(buf3)</code></a></td> + <td>1024</td> + </tr> +</table> +<p class="Pp">The examples above are the same for ILP32 and LP64 platforms, as + they are based on character units.</p> +<p class="Pp">When applied to a struct or union, <code class="Nm">sizeof</code> + returns the total number of bytes in the object, including any internal or + trailing padding used to align the object in memory. This result may thus be + larger than if the storage size of each individual member had been + added:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>struct s1 { + char c; +}; + +struct s2 { + char *s; + int i; +}; + +struct s3 { + char *s; + int i; + int j; +}; + +struct s4 { + int i; + uint64_t i64; +}; + +struct s5 { + struct s1 a; + struct s2 b; + struct s3 c; + struct s4 d; +};</pre> +</div> +<table class="Bl-column Bd-indent"> + <tr id="Object~3"> + <td><a class="permalink" href="#Object~3"><b class="Sy">Object or + type</b></a></td> + <td><b class="Sy">Result (ILP32)</b></td> + <td><b class="Sy">Result (LP64)</b></td> + </tr> + <tr id="sizeof(struct"> + <td><a class="permalink" href="#sizeof(struct"><code class="Li">sizeof(struct + s1)</code></a></td> + <td>1</td> + <td>1</td> + </tr> + <tr id="sizeof(struct~2"> + <td><a class="permalink" href="#sizeof(struct~2"><code class="Li">sizeof(struct + s2)</code></a></td> + <td>8</td> + <td>16</td> + </tr> + <tr id="sizeof(struct~3"> + <td><a class="permalink" href="#sizeof(struct~3"><code class="Li">sizeof(struct + s3)</code></a></td> + <td>12</td> + <td>16</td> + </tr> + <tr id="sizeof(struct~4"> + <td><a class="permalink" href="#sizeof(struct~4"><code class="Li">sizeof(struct + s4)</code></a></td> + <td>12</td> + <td>16</td> + </tr> + <tr id="sizeof(struct~5"> + <td><a class="permalink" href="#sizeof(struct~5"><code class="Li">sizeof(struct + s5)</code></a></td> + <td>36</td> + <td>56</td> + </tr> +</table> +<p class="Pp" id="without">When applied to a struct containing a flexible array + member, <code class="Nm">sizeof</code> returns the size of the struct + <a class="permalink" href="#without"><i class="Em">without</i></a> the + array, although again possibly including any padding the compiler deemed + appropriate:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>struct flex { + char c; + long b; + char array[]; +}</pre> +</div> +<table class="Bl-column Bd-indent"> + <tr id="Object~4"> + <td><a class="permalink" href="#Object~4"><b class="Sy">Object or + type</b></a></td> + <td><b class="Sy">Result (ILP32)</b></td> + <td><b class="Sy">Result (LP64)</b></td> + </tr> + <tr id="sizeof(struct~6"> + <td><a class="permalink" href="#sizeof(struct~6"><code class="Li">sizeof(struct + flex)</code></a></td> + <td>8</td> + <td>16</td> + </tr> +</table> +<p class="Pp">One of the more common uses of the <code class="Nm">sizeof</code> + operator is to determine the correct amount of memory to allocate:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>int *nums = calloc(512, sizeof(int));</pre> +</div> +<p class="Pp">The <code class="Nm">sizeof</code> operator can be used to + calculate the number of elements in an array by dividing the size of the + array by the size of one of its elements:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>int nums[] = { 1, 2, 3, 4, 5 }; +const int howmany = sizeof(nums) / sizeof(nums[0]);</pre> +</div> +<p class="Pp">Many systems provide this shortcut as the macro + <code class="Dv">ntimes()</code> via the + <code class="In"><<a class="In">sys/param.h</a>></code> header + file.</p> +</section> +<section class="Sh"> +<h1 class="Sh" id="RESULT"><a class="permalink" href="#RESULT">RESULT</a></h1> +<p class="Pp">The result of the <code class="Nm">sizeof</code> operator is an + unsigned integer type, defined in the <code class="Dv">stddef.h</code> + header as a <var class="Vt">size_t</var>.</p> +</section> +<section class="Sh"> +<h1 class="Sh" id="NOTES"><a class="permalink" href="#NOTES">NOTES</a></h1> +<p class="Pp">It is a common mistake to apply <code class="Nm">sizeof</code> to + a dynamically allocated array:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>char *buf; +if ((buf = malloc(BUFSIZ)) == NULL) { + perror("malloc"); +} +/* Warning: wrong! */ +(void)strncat(buf, input, sizeof(buf) - 1);</pre> +</div> +<p class="Pp">In that case, the operator will return the storage size of the + pointer ( ‘<code class="Li">sizeof(char *)</code>’ ), not the + allocated memory.</p> +<p class="Pp" id="does"><code class="Nm">sizeof</code> determines the + <code class="Ev">size</code> of the result of the expression given, but + <a class="permalink" href="#does"><i class="Em">does not</i></a> evaluate + the expression:</p> +<div class="Bd Pp Bd-indent Li"> +<pre>int a = 42; +printf("%ld - %d\n", sizeof(a = 10), a); /* Result: "4 - 42" */</pre> +</div> +<p class="Pp">Since it is evaluated by the compiler and not the preprocessor, + the <code class="Nm">sizeof</code> operator cannot be used in a preprocessor + expression.</p> +</section> +<section class="Sh"> +<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE + ALSO</a></h1> +<p class="Pp"><a class="Xr">arch(7)</a>, <a class="Xr">operator(7)</a></p> +</section> +<section class="Sh"> +<h1 class="Sh" id="STANDARDS"><a class="permalink" href="#STANDARDS">STANDARDS</a></h1> +<p class="Pp">The <code class="Nm">sizeof</code> operator conforms to + <span class="St">ANSI X3.159-1989 + (“ANSI C89”)</span>.</p> +<p class="Pp">Handling of flexible array members in structures conforms to + <span class="St">ISO/IEC 9899:1999 + (“ISO C99”)</span>.</p> +</section> +<section class="Sh"> +<h1 class="Sh" id="AUTHORS"><a class="permalink" href="#AUTHORS">AUTHORS</a></h1> +<p class="Pp">This manual page was written by <span class="An">Jan + Schaumann</span> + <<a class="Mt" href="mailto:jschauma@netmeister.org">jschauma@netmeister.org</a>>.</p> +</section> +</div> +<table class="foot"> + <tr> + <td class="foot-date">December 12, 2022</td> + <td class="foot-os">FreeBSD 15.0</td> + </tr> +</table> |
