0

From the official documentation on the memory keyword:

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

Can someone please clarify the meaning of between (external) function calls?

Does it mean between two function calls made from the offchain (e.g., via a web3 script)?

Or does it possibly mean that passing a memory array from a contract function to some other contract function is not reliable in the sense that the array will not be persistent across both functions?

To elaborate, my overall purpose is to change this:

uint[] private arr;
function func(uint arr0, uint arr1, uint arr2) public returns (uint256) {
    arr = [arr0, arr1, arr2];
    return otherContract.func(arr);
}

To this:

function func(uint arr0, uint arr1, uint arr2) public returns (uint256) {
    uint[] memory arr = new uint[](3);
    arr[0] = arr0;
    arr[1] = arr1;
    arr[2] = arr2;
    return otherContract.func(arr);
}

And although the first method works fine, I want to be sure that it is absolutely safe to do so, because I feel that using a storage array is a rather bad practice (let alone a more expensive method) in this case.

Thank you!

3

Neither your contract's storage nor its memory are shared with other contracts. What's happening in both code snippets is that the data you're passing to the other contract is encoded into call data and sent to the other contract that way.

  • Perfect. This means that the array data is persistent, right? By persistent, I mean that it "arrives safely" at otherContract.func (not that I can change it there, and the change will apply in the function of origin). – goodvibration Aug 25 at 19:27
  • That's not how I think of the word persistent, but yes, it will arrive safely. :-) – smarx Aug 25 at 19:28
  • TX!!!!!!!!!!!!!!!!! – goodvibration Aug 25 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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