Skip to content

Instantly share code, notes, and snippets.

@maurelian
Created February 15, 2018 17:06
Show Gist options
  • Save maurelian/c3f48742576d95bac2d72a139e13c4a1 to your computer and use it in GitHub Desktop.
Save maurelian/c3f48742576d95bac2d72a139e13c4a1 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.0;
contract C {
uint[] x; // the data location of x is storage
// the data location of memoryArray is memory
function f(uint[] memoryArray) public {
x = memoryArray; // works, copies the whole array to storage
var 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 storageArray) internal {}
function h(uint[] memoryArray) public {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment