0

If I call this function the transaction get pending and never finish. Any idea?

pragma solidity ^0.4.24;

contract test
{

uint[] values = [1,2,3,4,5,6];

function removeValueFromArray(uint _num) public returns(uint[]) {

    uint[] storage auxArray;

    for (uint i = 0; i < values.length; i++){
        if(values[i] != _num)
            auxArray.push(values[i]);
    }

    return auxArray;
}
}
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
UnexpectedCharacter
  • 852
  • 1
  • 11
  • 30
  • Pending doesn't mean that there would be something wrong with the code. It only means it hasn't been processed yet. So the transaction is still waiting for some node to process it. – Lauri Peltonen Feb 14 '19 at 07:48
  • The problem is still same till near 1 day – UnexpectedCharacter Feb 14 '19 at 07:49
  • Give us a link to the transaction and we can help you better – Lauri Peltonen Feb 14 '19 at 07:56
  • I deploy this contract into a Remix virtual blockchain. Test porpuse. That the full code – UnexpectedCharacter Feb 14 '19 at 08:04
  • Side note: Regardless of your problem, what is it exactly that you are trying to do here??? You build an auxiliary array and then return it... I guess that you copy this array into the original array outside the function. Why not work directly on the original array? I already mentioned that in a comment on a different (though almost identical) post of yours a few days ago. – goodvibration Feb 14 '19 at 10:50
  • BTW, it appears that for what you are trying to achieve, a mapping(uint => uint) could be a lot more suitable. You map each value to the number of times that it appears, and when you want to remove that value, then simply change its count to 0. – goodvibration Feb 14 '19 at 10:51

1 Answers1

1

You have an uninitialized storage pointer because you are declaring a storage array inside your function.

You should never do that because it will overwrite storage in unexpected ways. The issue of not returning is resolved by declaring it properly:

pragma solidity ^0.4.24;

contract test {

    uint[] values = [1,2,3,4,5,6];
    uint[] auxArray;

    function removeValueFromArray(uint _num) public returns(uint[]) {

        //uint[] storage auxArray;

        for (uint i = 0; i < values.length; i++){
            if(values[i] != _num)
                auxArray.push(values[i]);
        }

        return auxArray;
    }
}

Background on storage pointers: https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089

I imagine this is experimental. In Solidity, this is a bad approach to a delete/splice operation generally, because it relies on a for loop and a lot of moving data around. https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad

Have a look over here from some patterns, including one that does delete. Are there well-solved and simple storage patterns for Solidity?

Keep in mind that in practice, delete isn't used very often and truthfully, nothing on blockchains is every really deleted, although we can treat elements that way logically. I mention this because the delete setup adds complexity you might not need.

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145