0

I'm trying to understand value type and reference type concept in Solidity as per new changes. I'm trying understand local reference types with following example.

pragma solidity ^0.5.0;

contract Locations {

  function doSomething() public  {   

    uint[] storage localArray;  

  }
}

It gives me an error of array initialization. However after doing something like uint[] storage localArray = new uint[]; (which might also wrong) I found strange error

Type function (uint256) pure returns (uint256[] memory) is not implicitly convertible to expected type uint256[] storage pointer

Any thought?

1

You can not declare new storage variables inside functions. You can only declare new memory variables (see Storage variable inside a function)

  • Has this changed since 0.5.0? as its working in older version. – Div Feb 1 at 16:39
  • @Div hmm, that is interesting. What is the use case for doing this? – Rosco Kalis Feb 1 at 17:06
  • There is no specific use case! I just started learning Solidity and try to understand the things. – Div Feb 1 at 17:08
  • @Div In old versions of solidity it is allowed but it is an error because the uninitialized will point to the first storage slot. If you are not careful you might end up overwriting storage variables unintentionally. – Ismael Feb 2 at 6:59
  • 2
    @Div See this vulnerability for details smartcontractsecurity.github.io/SWC-registry/docs/SWC-109 – Ismael Feb 2 at 7:20

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.