Skip to content

Instantly share code, notes, and snippets.

@lyhistory
Created January 29, 2019 08:10
Show Gist options
  • Save lyhistory/a17ec9bc21a8fe85ff832de8f24b060f to your computer and use it in GitHub Desktop.
Save lyhistory/a17ec9bc21a8fe85ff832de8f24b060f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.17+commit.bdeb9e52.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract C {
uint[] x;
// the data location of x is storage
// the data location of memoryArray is memory
function f(uint[] memory memoryArray) public{
x = memoryArray; // works, copies the whole array to storage
uint[] storage y = x; // works, assigns a pointer, data location of y is storage
//y[7]; // fine, returns the 8th element
//y.length = 2; // fine, modifies x through y
//delete x; // fine, clears the array, also modifies y
// The following does not work; it would need to create a new temporary /
// unnamed array in storage, but storage is "statically" allocated:
// y = memoryArray;
// This does not work either, since it would "reset" the pointer, but there
// is no sensible location it could point to.
// delete y;
g(x);
// calls g, handing over a reference to x
h(x);
// calls h and creates an independent, temporary copy in memory
}
function g(uint[] storage) internal pure{}
function h(uint[] memory) public pure{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment