I needed a way to clear a storage array in my contract. I've seen this answer, but I tried to think of different approach (My arrays are most probably will contain enormous amount of elements) and have come up to this.
I create empty storage array of the same type, as the array I want to clear. Then I just assign empty array to filled array. Here is contract I used for testing.
pragma solidity ^0.4.18;
contract Test {
address[] array;
address[] helper;
function Test() public {
array.push(0x0);
array.push(0x0);
array.push(0x0);
array.push(0x0);
}
function getSize() public view returns (uint256) {
return array.length;
}
function clear() public {
array = helper;
}
}
It works as intended, getSize()
returns 0 after I call clear()
.
So now I wonder, if there are some hidden problems in this approach, including something like "dangling pointers" or increased gas cost.
Thank you in advance.