Data Location Assignment [OLD]

The update and the add user functions shouldn’t do the same. There is something wrong with your secret code…

good luck :upside_down_face:

Using memory, makes a copy of the struct in the memory and the value is updated in there. Using storage accually updates the “real” struct and we can now update the ballance

@filip
The obvious step is change (memory -> storage) but i thought the solution would be shorter like this:

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

Any way to know which of the two solutions is more efficient (Gas-wise I guess)?

---- Update

Already answered in the next video!! My doubt now is the version with two steps counts as two storage memory locations or is just the same as the 1 line code?¿

pragma solidity 0.5.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);
    }
//So I deleted the extra User memory stuff as we only need to change the users[id] in order to effect the storage
    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
    function updateBalance(uint id, uint balance) public {
         User memory updated_user = users[id];
         updated_user.balance = balance;
         
         users[id].balance = updated_user.balance;
    }
1 Like

But i guess the best one is just

users[id].balance = balance;

1 Like

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;

// or we can write : users[id].balance=balance;
}

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

}

1 Like

My solution would be to change this function.

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

pragma solidity ^0.5.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 {
     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;
}
1 Like

function updateBalance(uint id, uint balance) public {
//User memory user = users[id];
//user.balance = balance;
// just added this line - works but is kind of ugly
addUser(id, balance);
//this works as well and looks better
users[id].balance = balance;
}

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

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

}

I ve only changed user.balance = balance; to users[id].balance = balance;

It works but i still have yellow warning “unused local variable.”

Edit: nvm findout why in the solution video :smiley:

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

pragma solidity 0.5.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 {
     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.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 {
users[_id].balance = _balance; // By using struct in Storage, balance is updated correctly !!
// User memory user = users[_id];   <<----- PROBLEM: this struct is local;
// user.balance = _balance;   <<----- it does not stay in permanent memory
}
function getBalance(uint _id) view public returns (uint){
return(users[_id].balance);
}

}

Edit by @gabba, use the Preformatted text tag to display your code @Glenn_CostaRica it makes it easier to read :slight_smile:

1 Like

My solution is to replace the data location “memory” in the “updateBalance” function with “storage”, which means that the balance will be stored permanently, not just during the execution of the function.

1 Like

My fix - just one function:
function updateBalance(uint id, uint balance) public {
// User memory user = users[id];
users[id].balance = balance;
}

1 Like
pragma solidity 0.5.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; //solution for assignment
         
    }

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

}

Solution is designated with a comment in the code.

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

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

}
1 Like