Solidity theorytheory 0/50 · 0%
Types · easy

8. Arrays

Fixed and dynamic arrays, push, pop and length.

Arrays are either fixed (`uint256[3]`) or dynamic (`uint256[]`). Only dynamic storage arrays support `push` and `pop`; memory arrays have a fixed length once created.

address[] public stakers;

function join() external {
    stakers.push(msg.sender);
}

function pair() external pure returns (uint256[] memory) {
    uint256[] memory out = new uint256[](2);
    out[0] = 1; out[1] = 2;
    return out;
}

Unbounded loops over a growing array are a gas trap: the function can become impossible to call.

Check your understanding

  1. 1. Which arrays support `push`?

  2. 2. How do you create a 2-element memory array?

  3. 3. What is the risk of looping over an ever-growing array?