16

And if so, what is it?

Also, would it differ by dynamic and static arrays?

My initial guess is that:

  • a statically-declared array has a maximum number of slots equal to the largest uint, as in array[max_uint256].
  • a dynamically-declared array has no "practical" limit to its size (but perhaps a huge theoretical one: see link at bottom)

Possibly related

2 Answers 2

14

There are no limits in specification, so arrays may grow up to 2^256-1 elements.

7
  • 4
    It’s worth noting that even if such a large array could exist, traversing it would fail due to was constraints. Dec 12, 2017 at 1:13
  • 3
    @pabloruiz55, as in due to gas constraints ?
    – Andy B.
    Jul 16, 2018 at 17:44
  • 3
    Yes, I meant GAS constraints Jul 17, 2018 at 1:38
  • 1
    @ch3ll0v3k correct, I found out since posting the above comment that storage arrays do not have this limit. github.com/ethereum/solidity/issues/13071 Jun 11, 2022 at 21:37
  • 1
    @ch3ll0v3k I wrote this up in detail in a separate answer. Jun 13, 2022 at 21:49
2

There is a smaller limit on the number of elements that can be in a dynamic memory array than the number of elements that can be in a dynamic storage array or a non-dynamic storage array.

https://blog.soliditylang.org/2020/04/06/memory-creation-overflow-bug :

Starting from Solidity version 0.6.5 the maximum allocation size for dynamic memory arrays is 2**64-1. Attempting to allocate larger arrays now directly reverts.

However this is specifically for dynamic arrays that are allocated in memory (not storage), as a local variable in a function, of the form

contract A {
    function x() {
        uint256[] myArray = new uint256[](someLargeNumber);
    }
}

Storage arrays are actually just a hashmap (implemented internally using a trie), also with a length field that sets the upper bound for indices, and they can store up to 2**256-1 elements (given an Ethereum node the size of the universe...).

Storage arrays can be dynamic too:

contract B {
    uint256 dynamicStorageArray[];
    uint256 nonDynamicStorageArray[3];
}

But neither of these storage array forms has the limit of 2**64-1 elements, both can hold 2**256-1 elements.

Another notable difference: dynamic storage arrays support push() and pop(). Dynamic memory arrays do not support this.

Internally, both memory arrays and storage arrays store the length of the array in their first memory location, as a uint256 (so the limit on the number of elements in a dynamic memory array is imposed by compiler-generated logic, not by the inability to represent or store more than this number of elements). array.length always returns a uint256, no matter the type of array or where it is stored out of memory or storage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.