With for example

struct Foo {
    mapping(address => uint) b;

Foo[] foo;

if a function only deals with foo[4], can it reference it from storage without copying it? is that what

function c() {
    Foo memory p = foo[4];


1 Answer 1


To get a reference from a variable in the contract state you have to use storage qualifier

function c() {
    Foo storage p = foo[4];

Inside the c function all changes to p will update foo also.

  • it copies nothing, right? if I use memory, does it copy anything, or, is it a reference except it cannot be updated?
    – cotld
    Nov 6, 2018 at 6:23
  • @cotld If you use the memory qualifier it will create a copy in memory of the struct.
    – Ismael
    Nov 6, 2018 at 6:53
  • so its less work for the EVM to use storage? is storage cheaper in gas as well?
    – cotld
    Nov 6, 2018 at 6:56
  • @cotld Access to storage is more expensive, reading an uint from storage cost 200 gas, from memory is 3 gas, update an uint from storage cost 5000 or 20000 gas. But memory is very limited if you use too much the cost grows quickly. In practice you have to try different approaches to reduce gas consumption, ie use only storage, only memory or mixing storage and memory.
    – Ismael
    Nov 6, 2018 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.