Data Location Assignment

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;

        // users[id].balance += balance;

    }

    function getBalance(uint id) view public returns (uint) {

        return users[id].balance;

    }

}

This is my solution, I noticed after reading other posts that the solution I got in the comment seems to be the most appropriate because we are talking about an update and not directly rewriting the balance value.
Even though the solution works just as well.

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 {
         User storage user = users[id];
         user.balance = balance;
         // alternate solution : 
            //users[id].balance = balance;
    }

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

}
1 Like

I have 2 solutions since my first one gave me a warning but it still ran:

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);

}

/*

//Original

function updateBalance(uint id, uint balance) public {

     User memory user = users[id];

     user.balance = balance;

}

*/

/* // Soultion 1

function updateBalance(uint id, uint balance) public {

     users[1].balance = balance;

}

*/

// Soultion 2

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
//SPDX-License-Identifier: GPL-3.0
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

I simply switched memory for 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) 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;
    }

}
2 Likes
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;
    }

}
2 Likes

I tried 2 ways and both works:
1.

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

and

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

Hi, I just changed memory to storage and it works.

replace “memory” with “storage”, because memory stores a function, and storage store state variables, which can be altered. “user = users[id];” for updating balance is not a function, but it’s for storing state variables.

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 {
         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 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 {

     users[id].balance = balance;

}

function getBalance(uint id)

view

public

returns (uint) {

    return users[id].balance;

}

}

1 Like
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
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;


    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

I’m having trouble understanding exactly what the code is saying in the problem and the difference between that and solution 1 (if I understand solution 1 correctly then solution 2 is no problem, so we’ll just stick with 1). What might be confusing me is simply multiple objects with the same name. I’ll post both the problem and problem solution below with my comments. Can someone confirm whether I’m getting it or am I on the wrong track?

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;           //input user ID, get the user

    struct User{
        uint id;
        uint balance;                      //declared structs are aways written to storage
    }

    function addUser(uint id, uint balance) public {    //add new instance of the struct "user" to storage
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {   //declare new uints "id" and "balance" that has nothing to do with the uints ID and balance that compose the struct "user", as function inputs
         User memory user = users[id];          // add a new instance of the struct "user" to memory only until the function is finished executing
         user.balance = balance;                // change the balance of the new instance we have created in memory, only to disappear when the function is finished executing
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;             //here is where the mapping users comes in
    }

}





pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;           //input user ID, get the user

    struct User{
        uint id;
        uint balance;                      //declared structs are aways written to storage
    }

    function addUser(uint id, uint balance) public {    //add new instance of the struct "user" to storage
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {   //declare new uints "id" and "balance" that haave nothing to do with the uints ID and balance that compose the struct "user", as function inputs
         User storage user = users[id];          // retrieve struct user from storage that contains the uint id that matches the provided one 
         user.balance = balance;                // change the balance of the user in storage to whatever this function's uint "balance" is set to, then wipe this function's uint "balance" from memory when the function concludes
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;                             //here is where the mapping users comes in
    }

}
1 Like

Has you mention in your function.
User memory user = users[id]; // add a new instance of the struct "user" to memory only until the function is finished executing

Then
user.balance = balance; // change the balance of the new instance we have created in memory, only to disappear when the function is finished executing

So Indeed, this will not update the balance on the user mapping, to properly update the balance, we have 2 options:

  • Create an instance from the storage using: User storage user = users[id]; and then user.balance = balance;
  • Directly modify the mapping value using: users[id].balance = balance;

Hope I explain it properly for you, if not let me know :nerd_face:

Carlos Z

It sounds like I’m getting it for the most part, but I’m not sure what you mean by “create an instance from the storage”. The way I read this, it is retrieving a number from storage that already exists and then overwriting it, whereas if updateBalance has the object user in memory, it never touches the user that is in storage. No?