Blockchain Smart Contracts with Solidity · 课时

状态变量与存储布局

了解存储、内存和 calldata 之间的区别,以及状态变量如何持久保存在区块链上。

第 1 / 4 课12 个步骤

状态变量与存储布局 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are State Variables?

Welcome to understanding how data lives on the blockchain! In Solidity, state variables are special variables whose values are permanently stored in the contract's storage on the blockchain.

Think of them as the persistent memory of your smart contract. Their values are saved even after a function finishes executing and across different transactions.

Data Locations in Solidity

Solidity uses different data locations to manage where information is stored during contract execution. These locations have big impacts on gas costs and how your variables behave.

  • storage: Permanent state on the blockchain.
  • memory: Temporary data for function execution.
  • calldata: Read-only data for external function arguments.

Deep Dive into `storage`

Variables declared at the contract level (outside of any function) are automatically assigned to storage. These are your contract's state variables.

Data in storage is expensive to modify because every change is written to the blockchain, making it permanent and immutable (in the sense of its history).

`storage` in Action

This contract stores a number in a state variable. Notice how myNumber keeps its value between calls to setNumber and getNumber.

pragma solidity ^0.8.0;

contract StorageExample {
    uint public myNumber; // This is a state variable, lives in storage

    function setNumber(uint _num) public {
        myNumber = _num;
    }

    function getNumber() public view returns (uint) {
        return myNumber;
    }
}

Understanding `memory`

memory is a temporary data location. It's used for variables that exist only during a function call and are discarded once the function execution completes.

It's cheaper than storage for temporary data, making it ideal for arrays, strings, or structs that are created and used within a single function.

`memory` in Action

This function creates a string in memory, modifies it, and returns it. The string only exists for the duration of the function call.

pragma solidity ^0.8.0;

contract MemoryExample {
    function greet(string memory _name) public pure returns (string memory) {
        string memory greeting = "Hello, ";
        // Use abi.encodePacked for simple string concatenation
        return string(abi.encodePacked(greeting, _name, "!"));
    }
}

Introducing `calldata`

calldata is a special read-only data location used exclusively for external function parameters. It's even cheaper than memory because data is not copied into memory, just referenced.

You cannot modify calldata. It's typically used for large data passed to external functions, saving gas by avoiding copies.

`calldata` in Action

This external function takes a string as calldata. Notice it's read-only and efficient for external calls.

pragma solidity ^0.8.0;

contract CalldataExample {
    // This function can only be called externally
    function processData(bytes calldata _data) external pure returns (uint) {
        // You cannot modify _data directly, but can read its properties
        return _data.length; // Returns the byte length of the data
    }

    // Another example returning calldata directly
    function echoString(string calldata _message) external pure returns (string calldata) {
        return _message;
    }
}

Comparing Data Locations

Choosing the right data location is crucial for gas optimization and correct contract behavior:

  • Storage: Permanent, expensive, contract state.
  • Memory: Temporary, cheaper, within-function variables.
  • Calldata: Read-only, cheapest, external function arguments.

Always use the most restrictive and cheapest option possible.

How Storage is Organized

Solidity packs state variables into 256-bit (32-byte) storage slots to save space and gas. Smaller types (like uint8, bool) can be packed together into a single slot.

However, this packing can be complex and depends on the order of variable declarations. Understanding it helps optimize gas costs, especially with structs and arrays.

Data Location Quiz

Which data location is used for permanent storage of a contract's state variables on the blockchain?

Summary of Data Locations

Great job! You've learned about the fundamental data locations in Solidity: storage, memory, and calldata.

  • State variables live in storage, making them persistent.
  • memory is for temporary data during function execution.
  • calldata is for efficient, read-only external function arguments.

Understanding these helps you write more efficient and correct smart contracts. Next, we'll explore complex data structures like Mappings and Dynamic Arrays!

免费开始

用 AI 导师学习 Blockchain Smart Contracts with Solidity — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「状态变量与存储布局」课时是免费的吗?

是的 — 「状态变量与存储布局」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

「状态变量与存储布局」这节课中我会学到什么?

了解存储、内存和 calldata 之间的区别,以及状态变量如何持久保存在区块链上。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Blockchain Smart Contracts with Solidity 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「状态变量与存储布局」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?

能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 状态变量与存储布局
  2. 映射与动态数组
  3. 事件与日志数据
  4. 存储槽与 Gas 优化
← 返回 Blockchain Smart Contracts with Solidity