1
    pragma solidity ^0.4.24;

contract MemoryAndStorage {
    mapping(uint => User) users;
    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);   
    }

    function updateBalance(uint id, uint balance) public {
        User storage user = users[id];
        user.balance = balance;
    }

    function getBalance(uint id) public view returns (uint) {
        return users[id].balance;
    } 

}

I have questions about the updateBalance function.

Question 1) As we know , there're pointers (storage pointers and memory pointers) and there're actual datas. First I am interested, if both storage pointer and the data that pointer is actually pointing to are both saved in storage?

Question 2) so in a function updateBalance, where does user variable got created that's pointing to users[id]?

Question 3) if I wrote the User memory user = users[id]; would that variable got created in memory storage and it would watch the data that'is in storage ?

Question 4) If I write the following : User storage user = User(5,10); there's an error. why?

1

I think https://programtheblockchain.com/posts/2018/03/09/understanding-ethereum-smart-contract-storage/ will answer most of your questions.

The key takeaway is that smart contract storage is basically a huge array. So there's no "allocation" or "creation" involved. When you write User storage user = users[id], that just gives you a local reference to that location in storage.

If you wrote User memory user = users[id];, you would instead end up with an in-memory copy of that User.

User storage user = User(5,10); is a problem because User storage user declares a storage reference. (Think of it as a pointer to something in storage.) The right-hand side of the assignment should be something in storage so user can refer to that.

  • So a) if I write User storage user = users[id], where is user variable created? I think you said it got created nowhere. It's just a reference. how can't it be created somewhere? can't storage reference variable point to something that is in memory? – Giorgi Lagidze Aug 24 '18 at 15:18
  • In the compiled bytecode, the location in storage is probably pushed onto the stack somewhere at runtime. No, a storage reference can't point to something in memory. (It can only refer to storage.) – smarx Aug 24 '18 at 15:26
  • why do you think User storage user = User(5,10); the right side of this statement User(5,10) gets created in memory and not in storage? because it's in the function ? – Giorgi Lagidze Aug 24 '18 at 15:28
  • What would it mean to "create" it in storage? I think you're assuming some sort of allocation (finding a free spot in storage and writing the data there), which is not how storage works. – smarx Aug 24 '18 at 15:30
  • So I finally understand it somehow. The only part missing for me is where reference variables are stored. User storage user where is that user variable? on stack memory? i don't think so. That's what ethereum says : local variables of struct, array or mapping type reference storage by default. so user reference variable is also in storage , but it says no allocation happens on storage . then what do I assume? – Giorgi Lagidze Aug 24 '18 at 15:33

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.