0

Please help explain why a very simple function gets "Gas requirement of function <function_name>() high: infinite" warning in Remix. Here is my simple contract:

contract Simple_Contract
{
    uint[]      public m_raw;

    function addRaw(uint _id) public
    {
        m_raw[m_raw.length++] = _id;
    }

}

It seems that uint[] causes this problem. If it is a case, how to use a dynamic array in the contract?

0

You want m_raw.push(_id).

Right now, you're trying to write past the end of the array, so the function will always fail.

  • The warning is still there when using m_raw.push(_id). – Long Truong Jan 24 '18 at 8:28
  • I'm not sure what the reason is for the warning, but in testing this, calling the function only takes ~50,000 gas. – smarx Jan 24 '18 at 8:36
  • my guess would be that the warning might be due to the fact that you set a uint type without specifying the number of bits of the uint. Try with a specified uint like uint8 or uint256, i'm curious to know if it gets rid of the warning :p – Asone Jan 24 '18 at 9:30
  • @Asone, the warning is still there when changing to uint256. Actually, uint is an alias for uint256. – Long Truong Jan 24 '18 at 9:45
  • @smarx, I am not sure why it only took you ~50,000 gas to execute that function. On my side, from Javascript VM in Remix, it says that it takes (62572 gas as a transaction cost) + (41108 gas as an execution cost) = ~103,000 gas to execute that function. – Long Truong Jan 24 '18 at 10:04
0

It is a known problem of remix: when it is not able to forecast precisely the gas because some unknown data, it assigns infinite gas forecast and switch on the warning (it is a warning, nothing more!).

Here the problem is that the static parser cannot understand how huge can be your dynamic array.

Any string (that is a different type of dynamic array) causes the same problem as well.

This is the situation in remix, up to 0.7.2 version, that is the version currently online.

Your function does not have any particular problem or bug that is related with the warning/ simply accept to be warned, check that all is what you wanted it to be and switch off the gas warning.

It’s the right way to manage this.

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.