3

I have a function that gets an address array from another contract, conditionally deletes the msg.sender from the array, then saves the new array back to that contract.

Because the array backerList is in memory, i can't do backerList.length--;

But I cannot declare backerList as a storage array because memory array can't be converted to a storage one.

I am stuck here, what should I do?

 address[] memory backerList = syndicate.getBackerList();

 if(syndicate.individualTotalBacking(msg.sender) == 0){

        uint index;

        for(uint i=0; i<backerList.length; i++){
            if (backerList[i] == msg.sender){
                index = i;
            }

        //shifting array

            for(uint k=index; k<backerList.length-1; k++){
                backerList[k]=backerList[k+1];
            }

        }

        backerList.length--;
        syndicate.setBackerList(backerList);
    } else {}
  • What is the function signature of setBackerList? – Jesse Busman Jun 23 '18 at 10:03
  • setBackerList basically pass backerList[ ] to another contract called syndicate. I dont think this is the issue to be honest. – Panda Power Jun 23 '18 at 10:20
  • What I'm trying to ask is, is the parameter type of setBackerList an address[] memory? – Jesse Busman Jun 23 '18 at 10:22
  • good point, it was just declared as this function setBackerList(address[] newBackerList) public { ... } – Panda Power Jun 23 '18 at 10:29
3

Here's a way to do backerList.length--; on an address[] memory backerList by using inline assembly:

assembly { mstore(backerList, sub(mload(backerList), 1)) }

Some important points to remember:

  • Make sure this assembly code never runs when backerList.length == 0 (don't allow the array length to underflow)

  • Don't try to use this to increase the size of an array (by replacing sub with add)

  • Only use it on variables with a type like ...[] memory (for example, don't use it on a address[10] memory or address)

Disclaimer: The use of inline assembly is usually not recommended. Use it with caution and at your own risk :)

  • 1
    amazing and scary, how risky is it for me to use it? that would be a neat solution for me. – Panda Power Jun 23 '18 at 10:39
  • @RickChen I certainly recommend testing it thoroughly. The major concerns are: Don't use it on empty arrays. Only use it on variables with a type like ...[] memory. – Jesse Busman Jun 23 '18 at 10:43
-1

Yes you are correct, you can't reduce size of memory array explicitly, it's only applicable for storage array. But you can use delete operator to clear memory, which will save your gas.

However size of array will not change, it will remain same. You need to create extra variable to track size of array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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