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;
}
}
  • 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 at 7:48
  • The problem is still same till near 1 day – UnexpectedCharacter Feb 14 at 7:49
  • Give us a link to the transaction and we can help you better – Lauri Peltonen Feb 14 at 7:56
  • I deploy this contract into a Remix virtual blockchain. Test porpuse. That the full code – UnexpectedCharacter Feb 14 at 8: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 at 10:50
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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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