1

I have been testing and found that data is destroyed as follows.

    function getFreeWallets2(  string mybonusname) public view returns ( uint length, uint  k1, uint  length1) {

    bytes32 bonusNameBytes32 = stringToBytes32(mybonusname);
    require(Bonuses[bonusNameBytes32].bonusExists, "Bonus does not exist");
      length = Wallets.length;
      length1 = getNumberWallets();
    address[]  memory    FreeWallets;
      k1=0;
    for(uint i = 0; i < length; i++)
    {
       address mywallet = Wallets[i];
       if (!WalletBonuses[mywallet][bonusNameBytes32].bonusExists)
       {
           FreeWallets[k1]=mywallet;
           k1++;
       }
    }



}

Outcome - the return is 0,0,0 even though Wallets has data in it.

When I comment out the allocation line for the array, then the return is 1,1,1 which shows the actual value.

This is the offending line -

FreeWallets[k1]=mywallet;

Somehow this causes the data to be overwritten and then the whole function does not work.

    function getFreeWallets2(  string mybonusname) public view returns ( uint length, uint  k1, uint  length1) {

        bytes32 bonusNameBytes32 = stringToBytes32(mybonusname);
        require(Bonuses[bonusNameBytes32].bonusExists, "Bonus does not exist");
          length = Wallets.length;
          length1 = getNumberWallets();
        address[]  memory    FreeWallets; 
          k1=0;
        for(uint i = 0; i < length; i++)
        {
           address mywallet = Wallets[i];
           if (!WalletBonuses[mywallet][bonusNameBytes32].bonusExists)
           {
            //   FreeWallets[k1]=mywallet; <==== Commented out
               k1++;
           }
        }



    }

I tried all kinds of combinations with storage and memory and it just throws errors all the time regarding push or something else.

The design is easy enough (just to filter an array) but solidity has some issues in handling this.

Has anyone any suggestions?

1
  • It just returns the variables. There is no explicit return statement. Commented Oct 3, 2018 at 5:24

1 Answer 1

1

I think the problem is since you are not allocating sufficient space to a dynamic array FreeWallets,mywallet is not getting inserted an array.

Allocate sufficient space to FreeWallets array and then insert an element. For that, you need to change the code as follows while defining array FreeWallets-

address[] memory FreeWallets = new address[](Wallets.length);

Hope it helps.

3
  • This works in terms of the memory issue. Thanks. Is there a way to control the size of the output array? Now I get back a lot of zeros in FreeWallets for the non-allocated wallets. It would be better to say have wallets(size 10) and the freewallets(size 5) when 5 wallets were already allocated (walletsbonus has the allocations). Commented Oct 3, 2018 at 6:08
  • Welcome, glad to help you. If you want to control a size of an array then you need to create a dynamic arrayFreeWallets in storage and insert an element in a dynamic array using FreeWallets.push(mywallet). Commented Oct 3, 2018 at 6:21
  • Yes that was what I was trying, but I met with endless errors when the array was declared in the function. It works fine when declared outside the function. I have a workaround of counting the allocations and then allocating the memory. The other option is to pass around a lot of arrays filled with zeros. Commented Oct 3, 2018 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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