ตัวแปรสถานะและเค้าโครงพื้นที่จัดเก็บ
ทำความเข้าใจความแตกต่างระหว่างพื้นที่จัดเก็บ หน่วยความจำ และ calldata รวมถึงวิธีที่ตัวแปรสถานะคงอยู่บนบล็อกเชน
ตัวแปรสถานะและเค้าโครงพื้นที่จัดเก็บ เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. memoryis for temporary data during function execution.calldatais 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวแปรสถานะและเค้าโครงพื้นที่จัดเก็บ”
ทำความเข้าใจความแตกต่างระหว่างพื้นที่จัดเก็บ หน่วยความจำ และ calldata รวมถึงวิธีที่ตัวแปรสถานะคงอยู่บนบล็อกเชน คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวแปรสถานะและเค้าโครงพื้นที่จัดเก็บ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ตัวแปรสถานะและเค้าโครงพื้นที่จัดเก็บ
- แมปปิงและอาร์เรย์แบบไดนามิก
- เหตุการณ์และการบันทึกข้อมูล
- ช่องจัดเก็บและการเพิ่มประสิทธิภาพแก๊ส