In Ethereum Solidity what is the purpose of the memory keyword

0 votes

When looking at sample contracts, sometimes arrays are declared in methods with "memory" and sometimes they aren't. What's the difference?

Nov 15, 2018 in Blockchain by Christine
• 15,790 points
1,731 views

1 answer to this question.

0 votes

Without the memory keyword, Solidity tries to declare variables in storage.

Lead Solidity dev chriseth: “You can think of storage as a large array that has a virtual structure… a structure you cannot change at runtime - it is determined by the state variables in your contract”.

That is, the structure of storage is set in stone at the time of contract creation based on your contract-level variable declarations and cannot be changed by future method calls. BUT -- the contents of that storage can be changed with sendTransaction calls. Such calls change “state” which is why contract-level variables are called “state variables”. So a variable uint8 storagevar; declared at the contract level can be changed to any valid value of uint8 (0-255) but that “slot” for a value of type uint8 will always be there.

If you declare variables in functions without the memory keyword, then solidity will try to use the storage structure, which currently compiles, but can produce unexpected results. memorytells solidity to create a chunk of space for the variable at method runtime, guaranteeing its size and structure for future use in that method.

memory cannot be used at the contract level. Only in methods.

See the the entry "What is the memory keyword? What does it do?" in the FAQ. I quote it here:

The Ethereum Virtual Machine has three areas where it can store items.

The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.

The second is “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.

The third one is the stack, which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.

For almost all types, you cannot specify where they should be stored, because they are copied everytime they are used.

The types where the so-called storage location is important are structs and arrays. If you e.g. pass such variables in function calls, their data is not copied if it can stay in memory or stay in storage. This means that you can modify their content in the called function and these modifications will still be visible in the caller.

There are defaults for the storage location depending on which type of variable it concerns:

  • state variables are always in storage
  • function arguments are always in memory
  • local variables of struct, array or mapping type reference storage by default
  • local variables of value type (i.e. neither array, nor struct nor mapping) are stored in the stack
answered Nov 15, 2018 by Perry
• 17,100 points

Related Questions In Blockchain

0 votes
1 answer

What is the use of Ethereum's RLPx in Ethereum ecosystem?

So, RLPx is a protocol suite. It ...READ MORE

answered Jun 5, 2018 in Blockchain by Johnathon
• 9,090 points
2,165 views
0 votes
1 answer

What is the previous block hash of block #4390176 in Ethereum Blockchain

1,226,797,074,502,984,598,563 Hope this helps! To know more, Enroll with Blockchain ...READ MORE

answered Sep 7, 2020 in Blockchain by Sayali
9,312 views
0 votes
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,400 points
5,052 views
+1 vote
1 answer

what is use of msg.sender in solidity code?

msg.sender(address) function indicated the sender of the ...READ MORE

answered Apr 25, 2018 in Blockchain by Shashank
• 10,400 points
9,138 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,428 views
+1 vote
1 answer

What is the best way to search for an item in blockchain?

All transactions and records in blockchain are ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points
1,900 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP