I'm struggling to find any documentation that describes how to create a local memory copy of a global uint256 variable. Consider the following example:

contract MyContract {
    uint256 myInt = 0;

     function setter(uint256 newValue) external {
         myInt = 5;

     function getAndReset() external returns (uint256) {
         uint256 value = myInt; //This appears to be a reference
         myInt = 0;
         return value; //This always returns zero, even if setter() has been called

In this case, the variable "value" in the getAndReset() function appears to be a reference to the global storage variable "myInt", and any changes to value results in myInt also being changed. How can I create a local memory copy of myInt so that the above code functions properly?

  • The contract works as expected uint256 value = myInt copies the value, it isn't a reference.
    – Ismael
    Feb 27 at 3:53

1 Answer 1


The error in the code is that value is being assigned as a reference to myInt, resulting in value always reflecting the current state of myInt. To fix this, directly assign myInt to value to create a local copy.

Code Snippet:

function getAndReset() external returns (uint256) {
    uint256 value = myInt; // Create a local copy of myInt
    myInt = 0; // Reset myInt
    return value; // Return the local copy
  • I can see that I missed the returns type off the getAndReset() function signature, thanks for pointing that out - I've now fixed it. But beyond that I don't see a difference between your function body and mine.
    – jambolina
    Feb 24 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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