solidity 一个address[]里面可以存储多少个地址
How many addresses can be stored inside an address [ ] in solidity
我正在建立一个合约,要求新用户准确发送 0.1 个以太币才能进入投资回合。
我已经为此苦苦挣扎了一段时间,我不知道如何在合约中存储新投资者的地址,以便以后可以使用 "address index"。
就我所见,不推荐使用动态数组,因为它们很容易使用 "too much gas" 并使合约永远卡住。
- 我怎么知道 address [ ] 中实际上可以存储多少个地址?
我想可以做一个简单的测试,但我不确定如何做。
这是我正在使用的代码。它基于伟大的 article Rob Hitchens 所写。
address[] userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
动态数组中可以存储的项目数量没有限制。 (从技术上讲,极限是 2^256,但这是已知宇宙中原子数量的数量级。)
数组中的项数根本不影响 gas 使用量。 能消耗大量gas的是枚举一个大数组。 (在这种情况下,重要的是你正在通过 运行 一个长循环执行大量代码。)
我正在建立一个合约,要求新用户准确发送 0.1 个以太币才能进入投资回合。
我已经为此苦苦挣扎了一段时间,我不知道如何在合约中存储新投资者的地址,以便以后可以使用 "address index"。
就我所见,不推荐使用动态数组,因为它们很容易使用 "too much gas" 并使合约永远卡住。
- 我怎么知道 address [ ] 中实际上可以存储多少个地址?
我想可以做一个简单的测试,但我不确定如何做。
这是我正在使用的代码。它基于伟大的 article Rob Hitchens 所写。
address[] userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
动态数组中可以存储的项目数量没有限制。 (从技术上讲,极限是 2^256,但这是已知宇宙中原子数量的数量级。)
数组中的项数根本不影响 gas 使用量。 能消耗大量gas的是枚举一个大数组。 (在这种情况下,重要的是你正在通过 运行 一个长循环执行大量代码。)