Data Location Assignment [OLD]

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

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

}

Hope i post in the corrct topic

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

}


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

}

1 Like

Hi @paschkka86

As you are not using this variable:

     User memory user = users[id];

I think you can remove it from your contract because it has no effect and cost gas for nothing :slight_smile:

1 Like

I’ve overwritten the mapping. Not shure if this is correct

pragma solidity 0.5.1;
contract MemoryAndStorage {

    mapping(uint => User) public 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;
        assert(user.id == id);
        users[id] = User(id, balance);
    }

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

Hi @Daniel_Fuchs
yes this is correct

Hi @gabba

Yes, your right. Seems like i didn’t thought it trough completely… Thanks for the tip

1 Like

Hi
All I did was to change the memory which i believe only executes during the function with storage which keeps the state variable, permanent until it is updated

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

}

1 Like

without reading anything here… I’m probably posting for the 100th time the same thing

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

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

}

2 Likes
pragma solidity 0.5.1;
contract MemoryAndStorage {
    // users mapping has a key of uint to the value User
    mapping(uint => User) users;
    // User struct has id and balance arguments
    struct User{
        uint id;
        uint balance;
    }
    // vars are either storage or memory. 
    //memory is local to the function, storage are alive for the whole contract

    
    // to add user we will input the users id and balance
     // creating the mapping
    // mapping[key] = value
    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    // issue is that update balance isn't updating
    // want to update balance part of a struct within a mapping 
    function updateBalance(uint id, uint balance) public {
        // get the val part of the mapping
         User memory user = users[id];
         //change the balance of the local memory variable
         user.balance = balance;
         // update the mapping to include the change
         users[id] = user;
    }

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

}

Edit @ivga80 (You can use the preformatted text button to show the code in the post).

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

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

}

2 Likes

I changed the memory location of the user in the updateBalance function from memory to storage since storing it in memory makes it so that the value is discarded after the function is finished, with storage the value is stored in a permanent location

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

}
1 Like
pragma solidity 0.5.1;
contract MemoryAndStorage {
    struct User{
        uint id;
        uint balance;
    }

    mapping(uint => User) users;
    

    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

After “mapping” around the internet for the result, I find out the solution was pretty simple.

  • storage: Everything is saved permanently. (Until is updated off course)

  • memory: Only saved during the function execution. Infinitely expandable temporary byte array.

  • stack: It holds a local variables of value types. Non-persistent data maintained by EVM (Ethereum Virtual Machine). EVM uses stack data location to load the variables during execution (similarly to the memory) . Stack location has the limitation up to 1024 elements and contains words of 256 bits, therefore we use mainly for integers.

spoiler

I changed the memory to storage

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

Wow, even with the code in a dropdown list.
I’m impressed. :pray:
Ivo

2 Likes

Just went and changed the mapping directly directly.

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

    function getBalance(uint id) view public returns (uint) {
        return users[id].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 memory user = users[id];
         user.balance = balance;// the mapping is left unupdated here, temporary memory values are changed but not saved
         users[id].balance=user.balance; // added this to update persistent mapping value
         //or replace 3 lines of code with users[id]=User(id,balance); as in addUser function
    }

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

// Changed memory to storage
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