<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 數據位置(Data location) 復雜類型,如`數組(arrays)`和`數據結構(struct)`在Solidity中有一個額外的屬性,數據的存儲位置。可選為`memory`和`storage`。 `memory`存儲位置同我們普通程序的內存一致。即分配,即使用,越過作用域即不可被訪問,等待被回收。而在區塊鏈上,由于底層實現了圖靈完備,故而會有非常多的狀態需要永久記錄下來。比如,參與眾籌的所有參與者。那么我們就要使用`storage`這種類型了,一旦使用這個類型,數據將永遠存在。 基于程序的上下文,大多數時候這樣的選擇是默認的,我們可以通過指定關鍵字`storage`和`memory`修改它。 默認的函數參數,包括返回的參數,他們是`memory`。默認的局部變量是`storage`的。而默認的狀態變量(合約聲明的公有變量)是`storage`。 另外還有第三個存儲位置`calldata`。它存儲的是函數參數,是只讀的,不會永久存儲的一個數據位置。`外部函數`的參數(不包括返回參數)被強制指定為`calldata`。效果與`memory`差不多。 數據位置指定非常重要,因為不同數據位置變量賦值產生的結果也不同。在`memory`和`storage`之間,以及它們和`狀態變量`(即便從另一個狀態變量)中相互賦值,總是會創建一個完全不相關的拷貝。 將一個`storage`的狀態變量,賦值給一個`storage`的局部變量,是通過引用傳遞。所以對于局部變量的修改,同時修改關聯的狀態變量。但另一方面,將一個`memory`的引用類型賦值給另一個`memory`的引用,不會創建另一個拷貝。 ``` pragma solidity ^0.4.0; contract DataLocation{ uint valueType; mapping(uint => uint) public refrenceType; function changeMemory(){ var tmp = valueType; tmp = 100; } function changeStorage(){ var tmp = refrenceType; tmp[1] = 100; } function getAll() returns (uint, uint){ return (valueType, refrenceType[1]); } } ``` 下面來看下官方的例子說明: ``` pragma solidity ^0.4.0; contract C { uint[] x; // the data location of x is storage // the data location of memoryArray is memory function f(uint[] memoryArray) { x = memoryArray; // works, copies the whole array to storage var y = x; // works, assigns a pointer, data location of y is storage y[7]; // fine, returns the 8th element y.length = 2; // fine, modifies x through y delete x; // fine, clears the array, also modifies y // The following does not work; it would need to create a new temporary / // unnamed array in storage, but storage is "statically" allocated: // y = memoryArray; // This does not work either, since it would "reset" the pointer, but there // is no sensible location it could point to. // delete y; g(x); // calls g, handing over a reference to x h(x); // calls h and creates an independent, temporary copy in memory } function g(uint[] storage storageArray) internal {} function h(uint[] memoryArray) {} } ``` ## 總結 ### 強制的數據位置(Forced data location) - `外部函數(External function)`的參數(不包括返回參數)強制為:`calldata` - `狀態變量(State variables)`強制為: `storage` ### 默認數據位置(Default data location) - 函數參數(括返回參數:`memory` - 所有其它的局部變量:`storage` 更多請查看關于數據位置的進一步挖掘: [http://me.tryblockchain.org/solidity-data-location.html](http://me.tryblockchain.org/solidity-data-location.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看