I have four questions. I hope you can help me. Good idea questions, maybe this will help others. Thanks in advance.

Question 1) I know that if you don't write anything to storage, then That function is free of gas. right? i can even make public view function and make thouthands for loop, but since there's no reason to distribute this change to other nodes, it must be free even though for loop is big and even though i am making new variables in memory in that function. Is my idea right?

Question 2) I made the public view function, but in that function I wrote something like writing a number in an array and then after writing it , i returned new added element from that array. I touched the storage, I wrote it there. but because of I named my function "view" , it didn't ask me to put gas in metamask. there was no metamask needed at all, even though i wrote in storage. why? My opinion is that It works for me, but not for others and it wouldn't propagate across the nodes.

Question 3) Everyone says you can't return dynamic arrays from functions. Let's take uint[] array. It's dynamic, but I can still return it. then what do people mean? I know i can't return multidimensional array and structs of array. but I can return uint[].

Question 4) I know that If there's some data in storage for example uint[] x . now If I write in a function something like that : uint[] memory mytest = x; That x data that was in storage gets copied into memory. Am I right? If not? why? if yes, does it work the way around? to copy from memory to storage? But i don't think so, because you can't write anything in storage later unless you specify at run time.

1 Answer 1

  1. Yes.
  2. When you call a view function, the method eth_call is used instead of eth_sendTransaction. A call is performed locally, and any state changes are discarded. So any writes to storage you made simply don't persist.
  3. "Everyone says you can't return dynamic arrays from functions." Then "everyone" is wrong. Perhaps if you cited a source, someone could help you to figure out what was meant.
  4. Yes, that copies from storage to memory. Yes, the other way works too:

    pragma solidity ^0.4.24;
    contract Test {
        uint256[] public foo;
        constructor() public {
            uint256[] memory bar = new uint256[](3);
            bar[0] = 1;
            bar[1] = 2;
            bar[2] = 3;
            foo = bar;
  • The last answer, do you think it's correct. I think it copies from memory to storage.
    – Chemistry
    Commented Aug 25, 2018 at 15:45
  • Correct. The code in my post copies from memory to storage. You asked if the code you shared copied from storage to memory. I said that yes, it does. Then you asked if the other way around could work, and I said that yes, that works too. Then I provided an example.
    – user19510
    Commented Aug 25, 2018 at 16:02
  • 1 is wrong. Not only writing to storage costs gas, a transaction containing just a loop will consume gas as well Commented Aug 26, 2018 at 11:38
  • @TjadenHess We're talking about view functions here, executed via eth_call (not a transaction).
    – user19510
    Commented Aug 26, 2018 at 15:41
  • I don't see that in the question, although it may be implied. view functions may be called via transactions as well, e.g. by another contract, in which case it will still cost gas Commented Aug 26, 2018 at 16:13

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.