Skip to content

Instantly share code, notes, and snippets.

@DaveAppleton
Last active February 16, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveAppleton/c13257ba5b68e04b4cfdbce501c9293e to your computer and use it in GitHub Desktop.
Save DaveAppleton/c13257ba5b68e04b4cfdbce501c9293e to your computer and use it in GitHub Desktop.
Memory vs Storage
pragma solidity ^0.4.19;
contract TestStorage {
struct St {
uint256 amountPaid;
uint256 amountOut;
bool registered;
}
mapping(address => St) store;
function addMemFunds() public payable {
St memory myStuff = store[msg.sender];
myStuff.amountPaid += msg.value; // and the other members
store[msg.sender] = myStuff;
}
function addMemFail() public payable {
St memory myStuff = store[msg.sender];
myStuff.amountPaid += msg.value; // and the other members
}
function addStorFunds() public payable {
St storage myStuff = store[msg.sender];
myStuff.amountPaid += msg.value;
}
function mine() public view returns (uint256 toPay, uint256 paid) {
St memory thisSt = store[msg.sender];
toPay = thisSt.amountPaid;
paid = thisSt.amountOut;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment