Data Location Assignment

My solution – changes made only to the updateBalance function:

pragma solidity 0.8.3;
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 memory user = users[id];
         //user.balance = balance;
         
         users[id].balance = balance; //Updated statement
    }

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

}
1 Like
pragma solidity 0.7.5;
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) view public returns (uint) {
        return users[id].balance;
    }

}


1 Like
pragma solidity 0.8.1;

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 {
         users[id].balance = balance;
    }

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

}
1 Like
pragma solidity ^0.7.5;

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) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;
    
    uint userBal;

    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 memory user = users[id];
         users[id].balance = balance;
         // I just changed user.balance to users[id].balance
    }

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

}

After reading other solutions, the obvious and more to the question one would have been replacing ‘User memory user’ to ‘User storage user’.

1 Like
pragma solidity 0.7.5;
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 {
        users[id].balance = balance;
    }

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

}
1 Like
pragma solidity 0.7.5;
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 {
        // Solution: changed "memory" to "storage" to make it saved as persisent data.
         User storage user = users[id];
         user.balance = balance;
    }

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

}
1 Like
pragma solidity 0.7.5;
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 memory  users[id] = user;user = users[id];
         user.balance = balance;
         
         users[id] = user;
    }

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

}

1 Like

In the update balance function, replace “memory” with “storage”.

pragma solidity 0.7.5;
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;
}

}

1 Like

pragma solidity 0.7.5;
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 {
     users[id].balance = balance;
}

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

}

1 Like
pragma solidity 0.7.5;

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 {
     users[id] = User(id, balance);
    }

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

    }

}

1 Like
pragma solidity 0.7.5;
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) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like
    function updateBalance(uint id, uint balance) public {
         users[id].balance = balance;
    }

This is how I updated the code!!

1 Like

After some trial and error I changed the memory location assignment in line 16 to storage and it started to work. I am still not fully understanding why, I am guessing it will be explained in the next video.

pragma solidity 0.7.5;
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) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like

Hi @CMar10,

Do you now have a better understanding of what is happening when we change memory to storage in this assignment, and why it is one of several ways to solve the problem? Hopefully, the solution video helped. But if not, let us know, and we’ll do our best to clear things up for you :slightly_smiling_face:

    function updateBalance(uint id, uint balance) public {
         users[id].balance = balance;
    }
1 Like

Thanks for replying, I think I have a better idea of why this works now. Since it was written to memory it was deleted before the program completed, whereas writing to storage keeps the the updated amount after completion.

1 Like

Hi @CMar10,

Yes… the new balance value was lost when the function finished executing, because it wasn’t saved to persistent storage in the mapping.

User storage user = users[id];

This line creates a pointer (a reference) to a specific User instance in the users mapping — the one which has, as its key, the id parameter input into the function. This means that when balance is assigned to user.balance in the second line, it is effectively being assigned to users[id] in the mapping:

user.balance = balance;

So, yes… this means the update is now made to persistent storage, instead of just being saved temporarily in memory within the function until the function finishes executing.

pragma solidity 0.5.12;

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) view public returns (uint) {
        return users[id].balance;
    }
    
}

I changed one word in the updateBalance function; I changed memory to storage, so that the new balance wouldn’t be immediately lost upon completion of the function.

1 Like

Having watched the solution video, I see now that I was only looking to solve the immediate problem instead of looking to understand the context of the problem and rewrite the code efficiently. Gotta change my thinking!

1 Like