存储、内存与栈
EVM 数据位置
存储、内存与栈 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Where Data Lives in the EVM
The EVM has several distinct places to keep data, each with different costs and lifetimes.
The three core data locations are the stack, memory, and storage. Choosing the right one is key to writing efficient contracts.
The Stack
The stack is the EVM's scratchpad for computation. It holds up to 1024 values of 256 bits each.
Opcodes push and pop from the top of the stack. It is extremely cheap but tiny and temporary.
PUSH1 0x02 // [2]
PUSH1 0x04 // [2, 4]
MUL // [8]
// stack vanishes when the call endsMemory
Memory is a temporary, linear byte array that exists only during a single transaction call.
It is used for things too big for the stack, like building a string or an array. It resets to empty when the call finishes.
memory[0x00..0x20] = some value
// readable/writable during the call
// erased after execution endsStorage
Storage is the contract's permanent state, kept on the blockchain between transactions.
It is a key-value store mapping 256-bit slots to 256-bit values. State variables in a contract live here.
storage slot 0 -> owner address
storage slot 1 -> total supply
storage slot 2 -> paused flag
// persists forever on-chainCost Comparison
Cost rises sharply by location:
- Stack — cheapest, a few gas per op
- Memory — cheap, grows with size
- Storage — very expensive, especially first writes
Why Storage Is Expensive
Storage writes are costly because every node must persist them forever. A fresh storage write (SSTORE) can cost 20000 gas.
This is why developers minimize storage writes and prefer memory for temporary work.
Storage Slots and Packing
Storage is organized into 32-byte slots. Smaller variables can be packed into one slot to save gas.
For example, two uint128 values fit together in a single 256-bit slot.
slot 0: [ uint128 a | uint128 b ]
// two values packed into one slot
// saves a storage slot = saves gasCalldata
A fourth location, calldata, holds the read-only input data of a transaction or call.
It is the cheapest place to read function arguments and cannot be modified, making it ideal for inputs you only need to read.
Choosing the Right Location
Rules of thumb:
- Use the stack for short-lived computation
- Use memory for temporary structures
- Use storage only for data that must persist
- Use calldata for read-only inputs
Lifetime Summary
Each location has a different lifetime:
- Stack and memory — die when the call ends
- Storage — persists permanently on-chain
- Calldata — exists for the duration of the call, read-only
Putting It Together
The EVM separates data into stack, memory, storage, and calldata, ordered roughly from cheapest and shortest-lived to most expensive and permanent.
Smart developers keep persistent storage writes to a minimum.
Quick Check
Test your knowledge of EVM data locations.
Recap: Storage, Memory, Stack
You learned that:
- The stack is cheap, tiny, and temporary
- Memory is temporary scratch space for a call
- Storage is permanent but expensive
- Calldata is cheap, read-only input
Next: opcodes in detail.
常见问题解答
「存储、内存与栈」课时是免费的吗?
是的 — 「存储、内存与栈」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「存储、内存与栈」这节课中我会学到什么?
EVM 数据位置 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「存储、内存与栈」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Ethereum 虚拟机
- 存储、内存与栈
- 操作码
- 账户与状态