0

from https://docs.soliditylang.org/en/v0.8.11/types.html: " If you use a reference type, you always have to explicitly provide the data area where the type is stored" But later they give this example:

contract CrowdFunding {
    // Structs can also be defined inside contracts, which makes them
    // visible only there and in derived contracts.
    struct Campaign {
        address payable beneficiary;
        uint fundingGoal;
        uint numFunders;
        uint amount;
        mapping (uint => Funder) funders;
    }

    uint numCampaigns;
    mapping (uint => Campaign) campaigns;
    ...
  1. Why don't they provide the data area?
  2. They use both "data location" and "data area" - What's the difference?

1 Answer 1

2

Outside of functions, you declare the state variables. All of them are located in the storage and therefore it is not necessary to indicate where they are. Only inside functions or as parameters of functions you need to indicate where they are (for reference types).

You cannot create new state variables inside functions, but you need to specify if you are dealing with variables in the storage or in the memory in some cases. Imagine that you have the following code inside a function

   Monster memory monster1 = monsters[id1];
   monster1.life = _newlife;

   Monster storage monster2 = monsters[id2];
   monster2.life = _newlife;

After the execution of the function, the first monster (located in memory) will be erased (it is in the memory and the memory is volatile) and this monster will not be modified (you are dealing with a copy, not with the one that are in the storage). The second case is different. It points to the monster that is in the storage, and so its parameter will be saved to the blockchain.

1
  • OK, so it's not true what they wrote: "If you use a reference type, you always have to explicitly provide the data area where the type is stored" Commented Feb 25, 2022 at 2:52

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.