5

storage error is showing in line number 6 for string title,enter image description here If I add the storage keyword in between, how does it affect my contract? Does it affect my byte code or gas consumption in later use.

The code goes by:

pragma solidity ^0.4.8; 
contract token{ 
    struct video { address vidAddress; string title; uint initialSupply; } 
    mapping(address=>video)videos; 
    function add(address vidAddress,string title,uint initialSupply){ 
    video memory v; 
    v.vidAddress=vidAddress; 
    v.title=title; 
    v.initialSupply=initialSupply; 
    videos[vidAddress]=v; 
    } 
}

In particular, this snippet of code:

pragma solidity ^0.4.8; 
contract token{ 
    struct video { string title;  } 
    function add(){ 
    } 
}

produces this compiler warning on line 3:

 Warning: Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
    struct video { string title;  } 
                   ^----------^

in version 0.4.13 on remix

9
  • 3
    May you post the whole code, not as an image? :) So we can fast copy/past for testing purposes. Commented Jul 17, 2017 at 9:13
  • the code has little to do with it. If you make any contract with a basic structure in it in which you have a string type member, it throws this warning. The smart contract executes properly without any trouble but it still asks to add storage keyword in middle to silent this warning. Commented Jul 17, 2017 at 9:17
  • 2
    Yeh, that's true. Check out this topic: ethereum.stackexchange.com/questions/21255/… It seems to be a compiler bug Commented Jul 17, 2017 at 9:26
  • 1
    Probably next compiler version will take care of this. Commented Jul 17, 2017 at 9:30
  • 1
    @ShubhabrataMukherjee Upvoted. Good to hear people take warnings seriously. :) Commented Jul 17, 2017 at 9:32

1 Answer 1

1

From solidity documentation

Every complex type, i.e. arrays and structs, has an additional annotation, the “data location”, about whether it is stored in memory or in storage. Depending on the context, there is always a default, but it can be overridden by appending either storage or memory to the type. The default for function parameters (including return parameters) is memory, the default for local variables is storage and the location is forced to storage for state variables (obviously).

In simple terms, you can specify the location of your variables. The default is storage for member variables and memory is for function params. Since string is special type of array so same applies to strings to.

Again from the documentation:

Variables of type bytes and string are special arrays. A bytes is similar to byte[], but it is packed tightly in calldata. string is equal to bytes but does not allow length or index access (for now).

I don't think you explicitly need to specify memory before string vars. But if the solidity compiler forces you to do so, paste the exact code ( not an image) for replicating the problem at our end.

For reference of what memory is look at this.

In simple terms, memory is non-persisting whereas storage is.

2
  • pragma solidity ^0.4.8; contract token{ struct video { address vidAddress; string title; uint initialSupply; } mapping(address=>video)videos; function add(address vidAddress,string title,uint initialSupply){ video memory v; v.vidAddress=vidAddress; v.title=title; v.initialSupply=initialSupply; videos[vidAddress]=v; } } Commented Jul 17, 2017 at 9:33
  • Yup, I replicated the issue, this is the compiler bug. Although you are using an older version (current is 0.8.13), but the issue still persists. Hope in next release, this will be taken care of. Commented Jul 17, 2017 at 9:37

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