<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Snacks - malloc</title>
    <subtitle>Snack-size learning for a fast-paced world.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://snacks.devtestonly.uk/tags/malloc/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-09-28T04:53:34+00:00</updated>
    <id>https://snacks.devtestonly.uk/tags/malloc/atom.xml</id>
    <entry xml:lang="en">
        <title>Modern C: Storage</title>
        <published>2026-09-28T04:53:34+00:00</published>
        <updated>2026-09-28T04:53:34+00:00</updated>
        
        <author>
          <name>someone</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk/c/storage/"/>
        <id>https://snacks.devtestonly.uk/c/storage/</id>
        
        <content type="html" xml:base="https://snacks.devtestonly.uk/c/storage/">&lt;p&gt;This chapter covers how C manages memory through dynamic allocation, the rules of storage duration and initialization, and the precise definitions of object lifetimes.&lt;/p&gt;
&lt;h2 id=&quot;13-1-malloc-and-friends&quot;&gt;13.1 malloc and friends&lt;/h2&gt;
&lt;p&gt;Dynamic allocation allows programs to request memory on the fly to handle varying data sizes. The standard library &lt;code&gt;&amp;lt;stdlib.h&amp;gt;&lt;/code&gt; provides &lt;code&gt;malloc&lt;/code&gt;, &lt;code&gt;free&lt;/code&gt;, &lt;code&gt;calloc&lt;/code&gt;, &lt;code&gt;realloc&lt;/code&gt;, and &lt;code&gt;aligned_alloc&lt;/code&gt;. These functions operate with &lt;code&gt;void*&lt;/code&gt;, meaning the memory is just raw bytes until used.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1 #1 &amp;amp; #5:&lt;/strong&gt; Only use allocation functions with a size strictly greater than zero. Storage allocated through &lt;code&gt;malloc&lt;/code&gt; is uninitialized and has no type.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1 #2 &amp;amp; 13.1.1 #1:&lt;/strong&gt; Failed allocations result in a null pointer. Always check the return value!&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1 #3:&lt;/strong&gt; Prefer the use of &lt;code&gt;strndup&lt;/code&gt; over &lt;code&gt;strdup&lt;/code&gt; to prevent out-of-bounds reading.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1 #4:&lt;/strong&gt; Do not cast the return of &lt;code&gt;malloc&lt;/code&gt; and friends (e.g., &lt;code&gt;(int*)malloc(...)&lt;/code&gt; is bad practice in C).&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;string.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1 #1, #4, #5: Allocate memory without casting.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; length &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 5&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    double&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; largeVec &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; malloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;length &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span&gt;largeVec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1 #2: Check for failure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;largeVec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;        for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;size_t&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; length&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt;            largeVec&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Now the memory acquires an effective type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;largeVec&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1 #3: Prefer strndup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; source &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Hello World&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; safe_str &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; strndup&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;source&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Copies exactly 5 chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;safe_str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; safe_str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Prints &amp;quot;Hello&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;safe_str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-1-2-ensuring-consistency-of-dynamic-allocations&quot;&gt;13.1.2 Ensuring consistency of dynamic allocations&lt;/h2&gt;
&lt;p&gt;Memory leaks (loss of allocated objects) and memory corruption occur when allocations and deallocations do not match perfectly.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1.2 #1 &amp;amp; #2:&lt;/strong&gt; For every allocation (&lt;code&gt;malloc&lt;/code&gt;, &lt;code&gt;calloc&lt;/code&gt;, etc.), there must be exactly one &lt;code&gt;free&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1.2 #3:&lt;/strong&gt; Only call &lt;code&gt;free&lt;/code&gt; with pointers exactly as they were returned by the allocation functions. Never free variables, compound literals, or offsets of pointers.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; ptr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; calloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;10&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Do work...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1.2: Matching free.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Freeing (ptr + 1) or forgetting this step is catastrophic.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-1-3-flexible-array-members&quot;&gt;13.1.3 Flexible array members&lt;/h2&gt;
&lt;p&gt;A Flexible Array Member (FLA) is an array without a specified size declared as the &lt;em&gt;last&lt;/em&gt; member of a struct. It lets you allocate the struct and the array buffer in a single block of memory.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1.3 #1:&lt;/strong&gt; You must manually allocate enough storage to access the structure AND the array elements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.1.3 #2:&lt;/strong&gt; Consistency between a length member and the flexible array must be maintained manually.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stddef.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdint.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Struct with a flexible array member&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; length&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;[]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Flexible array must be the last member&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt; ua32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; len &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 10&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1.3 #1: Calculate total size (struct + array elements)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; size &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; offsetof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ua32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;uint32_t&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;len&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;size &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ua32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt; size &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ua32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ua32&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; ap &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; calloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;ap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.1.3 #2: Manually store the length to maintain consistency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;        ap&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span&gt; len&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;        ap&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;data&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 42&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;First element: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%u&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt; ap&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;data&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ap&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-2-storage-duration-lifetime-and-visibility&quot;&gt;13.2 Storage duration, lifetime, and visibility&lt;/h2&gt;
&lt;p&gt;Visibility dictates where a name is valid. Lifetime dictates when an object actually exists in memory.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2 #3:&lt;/strong&gt; Every definition creates a new, distinct object (even if shadowed).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2 #5 &amp;amp; #6:&lt;/strong&gt; Objects have a strict lifetime. Accessing an object outside its lifetime fails.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2 #7:&lt;/strong&gt; A compound literal has the same lifetime as a variable declared in the same scope.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; use_pointer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Value: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%f&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    double&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; ptr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #79C0FF);&quot;&gt; NULL&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Lifetime of &amp;#39;x&amp;#39; is bound to this inner block&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;        double&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 35&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ptr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        use_pointer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Valid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; use_pointer(ptr); // ERROR (Takeaway 13.2 #6): &amp;#39;x&amp;#39; is dead here.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.2 #7: Compound literal bound to the main() block&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    double&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; p_comp &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;double&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 99&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    use_pointer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;p_comp&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Valid until main() returns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-2-1-static-storage-duration&quot;&gt;13.2.1 Static storage duration&lt;/h2&gt;
&lt;p&gt;Objects with static storage duration live for the entire program execution.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2.1 #1:&lt;/strong&gt; Objects with static storage duration are &lt;em&gt;always&lt;/em&gt; initialized. If not explicitly initialized, they default to 0 (or null).&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;double&lt;/span&gt;&lt;span&gt; global_val&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; File scope: implicitly 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Block scope, but static lifetime: implicitly 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; double&lt;/span&gt;&lt;span&gt; block_static&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Global: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%f&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;, Static Local: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%f&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; global_val&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; block_static&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-2-2-automatic-storage-duration&quot;&gt;13.2.2 Automatic storage duration&lt;/h2&gt;
&lt;p&gt;Variables defined inside a block (without &lt;code&gt;static&lt;/code&gt;) have automatic storage duration.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2.2 #3, #4 &amp;amp; #5:&lt;/strong&gt; Use &lt;code&gt;register&lt;/code&gt; for critical local variables. You cannot use the &lt;code&gt;&amp;amp;&lt;/code&gt; operator on a &lt;code&gt;register&lt;/code&gt; variable, preventing aliasing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.2.2 #7 &amp;amp; #8:&lt;/strong&gt; Temporary objects (like arrays returned inside a struct) are read-only, and their lifetime ends immediately after the expression finishes evaluating.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; demo &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; unsigned&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; ory&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; demo &lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;get_demo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; demo&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; .ory &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;42&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.2.2 #5: register keyword for optimization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    register&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; int&lt;/span&gt;&lt;span&gt; counter &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    counter&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;++&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; int* p = &amp;amp;counter; // ERROR (Takeaway 13.2.2 #3): Cannot take address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.2.2 #7 &amp;amp; #8: get_demo().ory is a temporary read-only object.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; It dies completely on the next line.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Temporary: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%u&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; get_demo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;ory&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-3-object-lifetime&quot;&gt;13.3 Object Lifetime&lt;/h2&gt;
&lt;p&gt;Understanding exactly &lt;em&gt;when&lt;/em&gt; an object starts and stops existing is vital, especially inside loops and with Variable Length Arrays (VLAs).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.3 #1 &amp;amp; #2:&lt;/strong&gt; For standard block variables and compound literals, lifetime starts when the block scope is entered. Initializers are evaluated &lt;em&gt;each time&lt;/em&gt; the definition is met.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.3 #3:&lt;/strong&gt; For a VLA, lifetime starts only when the declaration is &lt;em&gt;encountered&lt;/em&gt; during execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 3&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;++&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.3 #2: This compound literal is evaluated and initialized&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; to a new value (0, 1, 2) on EVERY iteration.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;        size_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; ip &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;size_t&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%zu&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span&gt;ip&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;13-4-initialization&quot;&gt;13.4 Initialization&lt;/h2&gt;
&lt;p&gt;C does not automatically clean up memory for you unless it’s static/global.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.4 #1 &amp;amp; #2:&lt;/strong&gt; Static objects default to 0. Automatic (local) and allocated (&lt;code&gt;malloc&lt;/code&gt;) objects &lt;em&gt;must&lt;/em&gt; be initialized explicitly by you.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Takeaway 13.4 #3:&lt;/strong&gt; Systematically provide a dedicated initialization function (e.g., &lt;code&gt;type_init&lt;/code&gt;) for your complex data types.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdlib.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span&gt; numerator&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    unsigned&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span&gt; denominator&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt; rat&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.4 #3: Systematic initialization function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;rat&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; rat_init&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;rat&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; p&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; num&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; unsigned&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; den&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;        p&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;numerator&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span&gt; num&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;        p&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;denominator&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span&gt; den&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; p&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Return the pointer to allow chaining&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Takeaway 13.4 #2: Explicitly initializing dynamically allocated memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    rat&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span&gt; my_rat &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; rat_init&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;malloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;rat&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 13&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 7&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;my_rat&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Ratio: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%lld&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%llu&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt; my_rat&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;numerator&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt; my_rat&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;denominator&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;        free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;my_rat&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
</feed>
