Search code examples
ethereumsoliditysmartcontracts

How many addresses can be stored inside an address [ ] in solidity


I'm building a contract that requires new users to send exactly 0.1 ether in order to enter an investment round.

I've been struggling with this for a while an I don't know how to store the addresses of new investors inside the contract so I can use the "address index" later.

For what I saw, dynamic arrays are not recommended because they can easily use "too much gas" and get the contract stuck forever.

  • How can I know how many addresses can actually be stored inside address [ ] ?

I assume a simple test can be done but I'm not sure how to do it.

This is the code I'm using. It's based on the great article Rob Hitchens wrote.

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  

}

Solution

  • There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)

    The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)