Since dynamically increasing in-memory arrays is not allowed in Solidity, I thought about creating a helper method like the below to create a new array with one more element every time I wanted to increase the size. What is the disadvantage of doing this since everything is done in memory? Thank you.
contract ArrayTest {
function test() internal pure returns (address[] memory) {
address[] memory a = new address[](1);
a[0] = address(0);
a = push(a, address(1));
a = push(a, address(2));
a = push(a, address(3));
a = push(a, address(4));
a = push(a, address(5));
return a;
}
function push(address[] memory a, address addressToPush)
internal
pure
returns (address[] memory)
{
// create the new array with one element larger than the passed in array
address[] memory b = new address[](a.length + 1);
// copy the old array into the new array
for (uint256 i = 0; i < a.length; i++) {
b[i] = a[i];
}
// add the new element to the end
b[b.length - 1] = addressToPush;
// return the new array
return b;
}
}