TheReal Dude
TheReal Dude

Reputation: 41

Solidity Memory data location

I've been following my new journey into learning Solidity. Now I'm into a structs + Data locations course, and I came to a situation when I didn't use the exact same example as the instructor did, both works. I would like to know which one is technically more correct.

Concerning the way the instructor did, I understand that if that data variable already exists, filled with data, and we're using it on a Storage, that would be understandable but for the Memory case, I still don't get the meaning of it.

Here are the two exmples:

Mine:

struct PERSON {
        uint256 id;
        string name;

    }

PERSON public personList;

function updateNewPerson(uint256 _index, string memory _name) public {
        PERSON memory newUpdatedPerson;
        newUpdatedPerson.name = _name;
        personList[_index] = newUpdatedPerson;
    }

Instructor code:

  struct PERSON {
            uint256 id;
            string name;
    
        }

PERSON public personList;


    function updateNewPerson(uint256 _index, string memory _name) public {
            PERSON memory newUpdatedPerson = personList[index];
            newUpdatedPerson.name = _name;
            personList[_index] = newUpdatedPerson;
        }

This is the same example with Storage data location I'm reffering to:

function updateStoragePerson(uint256 _index, string memory _name) public {
        PERSON storage newUpdatedPerson = personList[_index];
        newUpdatedPerson.name = _name;
}

I'm trying to figure out the is there's any difference there and i think I'm starting to uderstand it, and please correct me if I'm wrong.

So in my example I'm basically just passing the new user insert new value, which is in this case _name, then when giving it back to the array handing it to its position through the index array.

When on the instructor example, he's passing through the array index case content, which is id and name, then insering the new name value from the function before passing back the data to the array, and in this case id and _name are identical.

Upvotes: 1

Views: 812

Answers (2)

NuMa
NuMa

Reputation: 608

What I can see is that, in your code, when you create a newUpdatedPerson, you are creating an empty variable of type PERSON, and then you only fill it with the name, whereas your instructor takes the original value from a person which already exists and then updates its value. This will result in your newUpdatedPerson in your personList to update its name but its id will become empty.

The newUpdatedPerson will be stored in personList as copies from storage to local storage actually copy a reference to the storage so any update on newUpdatedPerson will result in an update to personList.

Assignments from storage to a local storage variable also only assign a reference.

I hope you find this information helpful :)

Upvotes: 0

Yilmaz
Yilmaz

Reputation: 49351

  • State variables are storage by default (values are stored in the blockchain).

  • Local variables in functions are memory by default (values are stored temporarily in memory).

  • Structs are storage by default (values are stored in the blockchain).

I explained how memory and storage type behave differently on this stackoverflow post

Upvotes: 0

Related Questions