Does the memory operation require no gas at all? I knew it was a bit needed. But, memory operations works fine in the view function.

for example,

contract TestContract{

    uint testUint;

    function testFunc() public view returns(uint[]){
        uint[] memory testArr = new uint[](1);
        testArr[0]++;   // This line works fine.
        testUint++;     // This line not works.
        return testArr;

Why does the memory operation in the view function works?

I did not speak English well and I got help from translator. Thank you for your understanding.

2 Answers 2


Yes, memory functions take gas if used in a transaction.

View functions, when called, don't require gas. They are done for free on the node you're connected to. (But they can't change any state.)


Yes, but they require less gas than storage. See this form for more insight into gas cost: https://docs.google.com/spreadsheets/d/1n6mRqkBz3iWcOlRem_mO09GtSKEKrAsfO7Frgx18pNU/edit#gid=0. Also for the testUint, the line of code works but you are not returning it and for that reason not seeing any change in value. If you would like to return a single uint, change the return type from uint array to uint followed by returning the testUint.

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.