import{_ as i,c as a,a as n,o as l}from"./app-Dqab9jXZ.js";const e={};function h(t,s){return l(),a("div",null,[...s[0]||(s[0]=[n(`<h2 id="关于-memory-和-storage-存储类型" tabindex="-1"><a class="header-anchor" href="#关于-memory-和-storage-存储类型"><span>关于 memory 和 storage 存储类型</span></a></h2><ul><li><code>storage</code>：合约的持久化状态数据，保存在链上状态。对 <code>storage</code> 的写入最昂贵，读取也比内存贵；修改会永久生效。</li><li><code>memory</code>：函数调用期间的临时数据，函数返回后即释放。对 <code>memory</code> 的更改不会持久化。</li><li>（补充）<code>calldata</code>：外部函数参数的只读数据位置，零拷贝、不可修改，用于节省 gas。</li></ul><h3 id="生命周期与成本" tabindex="-1"><a class="header-anchor" href="#生命周期与成本"><span>生命周期与成本</span></a></h3><ul><li><code>storage</code> 写入昂贵、读取较贵；适合保存需要长期存在的状态。</li><li><code>memory</code> 在函数结束时释放，读取/写入相对便宜；适合临时计算与返回值。</li><li>复杂引用类型（数组、<code>struct</code>、<code>mapping</code>、<code>string</code>、<code>bytes</code>）在函数参数或局部变量处通常必须显式标注数据位置。</li></ul><h3 id="默认与必须声明" tabindex="-1"><a class="header-anchor" href="#默认与必须声明"><span>默认与必须声明</span></a></h3><ul><li>状态变量总是位于 <code>storage</code>（例如 <code>User[] public users;</code>）。</li><li>外部函数（<code>external</code>）的复杂类型参数默认是 <code>calldata</code>；内部/公共函数需要显式标注 <code>memory</code> 或 <code>storage</code>。</li><li>局部变量的复杂类型必须指定数据位置，否则编译报错。</li></ul><h3 id="拷贝与引用语义" tabindex="-1"><a class="header-anchor" href="#拷贝与引用语义"><span>拷贝与引用语义</span></a></h3><ul><li>从 <code>storage</code> 读取到 <code>memory</code> 会“复制”数据；修改 <code>memory</code> 副本不影响原始 <code>storage</code>。</li><li>使用 <code>storage</code> 局部变量可以得到对状态数据的“引用”，对其赋值会持久化。</li></ul><div class="language-solidity line-numbers-mode" data-highlighter="shiki" data-ext="solidity" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-solidity"><span class="line"><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;">pragma</span><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;"> solidity</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> ^0.8.20</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">contract</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> Users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    struct</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> User</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> string</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> name</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> uint</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> age</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> }</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">    User</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[]</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> public</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> add</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">string</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> memory</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> name</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">,</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> uint</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> age</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> external</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">.</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">push</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">User</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">name</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">,</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> age</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">));</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"> // 写入 storage</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> updateName</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">,</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> string</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> memory</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> newName</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> external</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        User </span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">storage</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> u </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">];</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;">   // storage 引用（指向链上状态）</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        u</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">.</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">name </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> newName</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;">            // 修改持久化生效</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> tryUpdate</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> external</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        User </span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">memory</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> u </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">];</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;">    // 从 storage 复制到 memory</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        u</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">.</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">age </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#2F798A;--shiki-dark:#4C9A91;"> 99</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;">                  // 仅修改副本，不会影响链上状态</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">    }</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">}</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在内部函数传递-storage-引用" tabindex="-1"><a class="header-anchor" href="#在内部函数传递-storage-引用"><span>在内部函数传递 storage 引用</span></a></h3><ul><li>仅内部/私有函数可以接收 <code>storage</code> 引用参数，从而直接修改状态；外部函数参数不能是 <code>storage</code>。</li></ul><div class="language-solidity line-numbers-mode" data-highlighter="shiki" data-ext="solidity" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-solidity"><span class="line"><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;">pragma</span><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;"> solidity</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> ^0.8.20</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">contract</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> Users2</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    struct</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> User</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> string</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> name</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> uint</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> age</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> }</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">    User</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[]</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> public</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> _inc</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">User</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> storage</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> u</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> internal</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> u</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">.</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">age </span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">+</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#2F798A;--shiki-dark:#4C9A91;"> 1</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> }</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> birthday</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> external</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#59873A;--shiki-dark:#80A665;">        _inc</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">users</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">]);</span><span style="--shiki-light:#A0ADA0;--shiki-dark:#758575DD;"> // 传递 storage 引用，持久化修改</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">    }</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">}</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="动态-memory-数组与限制" tabindex="-1"><a class="header-anchor" href="#动态-memory-数组与限制"><span>动态 memory 数组与限制</span></a></h3><ul><li>可在 <code>memory</code> 中构造动态数组：<code>new uint[](n)</code>；适合作为返回值或临时计算。</li><li><code>mapping</code> 只能存在于 <code>storage</code>，不能在 <code>memory</code> 中创建或拷贝。</li></ul><div class="language-solidity line-numbers-mode" data-highlighter="shiki" data-ext="solidity" style="--shiki-light:#393a34;--shiki-dark:#dbd7caee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#121212;"><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code class="language-solidity"><span class="line"><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;">pragma</span><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;"> solidity</span><span style="--shiki-light:#A65E2B;--shiki-dark:#C99076;"> ^0.8.20</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">contract</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> Arrays</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">    function</span><span style="--shiki-light:#59873A;--shiki-dark:#80A665;"> make</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">(</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> n</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> external</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> pure</span><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;"> returns</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> (</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[]</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;"> memory</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;"> a</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> {</span></span>
<span class="line"><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">        a </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;"> new</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;"> uint</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">[](</span><span style="--shiki-light:#B07D48;--shiki-dark:#BD976A;">n</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">)</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"><span style="--shiki-light:#1E754F;--shiki-dark:#4D9375;">        for</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> (</span><span style="--shiki-light:#2E8F82;--shiki-dark:#5DA994;">uint</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> i </span><span style="--shiki-light:#999999;--shiki-dark:#666666;">=</span><span style="--shiki-light:#2F798A;--shiki-dark:#4C9A91;"> 0</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> i </span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">&lt;</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> n</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> i</span><span style="--shiki-light:#AB5959;--shiki-dark:#CB7676;">++</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">)</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> a</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">[</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;">i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">]</span><span style="--shiki-light:#999999;--shiki-dark:#666666;"> =</span><span style="--shiki-light:#393A34;--shiki-dark:#DBD7CAEE;"> i</span><span style="--shiki-light:#999999;--shiki-dark:#666666;">;</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">    }</span></span>
<span class="line"><span style="--shiki-light:#999999;--shiki-dark:#666666;">}</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="常见坑与实践建议" tabindex="-1"><a class="header-anchor" href="#常见坑与实践建议"><span>常见坑与实践建议</span></a></h3><ul><li>给 <code>storage</code> 变量整体赋值会进行深拷贝或引用变更（依据类型），要明确拷贝成本与语义。</li><li>修改 <code>memory</code> 副本不会持久化；要修改链上状态请使用 <code>storage</code> 引用。</li><li>大型 <code>string/bytes/数组</code> 在 <code>memory↔storage</code> 间复制成本高，尽量减少不必要的复制。</li><li>外部函数能用 <code>calldata</code> 的地方尽量使用（只读参数），节省 gas。</li></ul>`,17)])])}const p=i(e,[["render",h]]),d=JSON.parse('{"path":"/programming/solidity/other/miscellaneous/","title":"一些没分类的小知识","lang":"zh-CN","frontmatter":{"title":"一些没分类的小知识","createTime":"2025/10/12 15:34:38","permalink":"/programming/solidity/other/miscellaneous/","description":"关于 memory 和 storage 存储类型 storage：合约的持久化状态数据，保存在链上状态。对 storage 的写入最昂贵，读取也比内存贵；修改会永久生效。 memory：函数调用期间的临时数据，函数返回后即释放。对 memory 的更改不会持久化。 （补充）calldata：外部函数参数的只读数据位置，零拷贝、不可修改，用于节省 gas...","head":[["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"一些没分类的小知识\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2025-10-20T12:25:23.000Z\\",\\"author\\":[]}"],["meta",{"property":"og:url","content":"https://notes.simengweb.com/programming/solidity/other/miscellaneous/"}],["meta",{"property":"og:site_name","content":"仲夏夜之梦"}],["meta",{"property":"og:title","content":"一些没分类的小知识"}],["meta",{"property":"og:description","content":"关于 memory 和 storage 存储类型 storage：合约的持久化状态数据，保存在链上状态。对 storage 的写入最昂贵，读取也比内存贵；修改会永久生效。 memory：函数调用期间的临时数据，函数返回后即释放。对 memory 的更改不会持久化。 （补充）calldata：外部函数参数的只读数据位置，零拷贝、不可修改，用于节省 gas..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2025-10-20T12:25:23.000Z"}],["meta",{"property":"article:modified_time","content":"2025-10-20T12:25:23.000Z"}]]},"readingTime":{"minutes":2.4,"words":721},"git":{"createdTime":1760427548000,"updatedTime":1760963123000,"contributors":[{"name":"祀梦","username":"","email":"3501646051@qq.com","commits":2,"avatar":"https://gravatar.com/avatar/6406a81eeddc359cf3d3ce018797689fc6d014ff06215c27d0210b42e8f5a8ab?d=retro"}]},"autoDesc":true,"filePathRelative":"notes/programming/solidity/other/miscellaneous.md","headers":[]}');export{p as comp,d as data};
