Let's say we have a contract that implements a Priority Queue in Solidity that looks like this:

pragma solidity ^0.4.23;
pragma experimental ABIEncoderV2;
contract PQ {
struct Node {
    int256 key;
    address value;
Node[7] private heapKeyValue;   
function insert(int256 key, address addr) public {
      .. Implementation ..
function get_min() public view returns (int256) {
    return heapKeyValue[0].key;
function heapify(uint8 idx) internal {

And another contract that uses this contract:

contract test_pq {
       function test() public returns (int256){
         PQ memory pq;
         for (uint8 i = 0; i < 10; i++)
         return pq.get_min();

Then what is the gas cost for calling the test() function in test_pq contract? Intuitively, the way I see it function state does not alter any state thus it should be zero gas cost. Is this the case?

Also I would appreciate if someone could explain to me what happens under the hood when the line PQ memory pq is executed


The functions that do not alter any state do not consume gas unless they are called from contracts. The reason this is true is that if a function does not alters the state you can execute it in the node without actually doing a transaction, but this is not the case because test is calling insert whihc if the implementation refers to the name will likely change the state.

As for the line PQ memory pq, this will throw an error in solidity, you should do PQ pq this defines an object that you can use to access PQ methods.

Hope this helps

| improve this answer | |
  • Thanks for the answer! My question is that ` PQ pq` is only a temporary variable in the function. As a result, any chance happening in this contract should not be persistent and thus it should not be stored in the blockchain. If the miner has access to the Priority Q contract/abi he can execute the whole function locally, without changing the blockchain at all. – davinci26 Jun 21 '18 at 14:31

test calls PQ.insert. The answer to your question depends on the implementation of insert. If, like the name suggests, it inserts a Node in the queue, the state is changed. That means test will have to be called in a transction, which costs gas.

| improve this answer | |

No, test function would cost you gas. Only read operations don't cost any gas, the reasoning being that any node can just look it up in its storage section. In your test, you are running a loop and then pq.insert inserts i into your heapKeyValue variable. This does cost you gas.

As for PQ memory pq, let's read from the Solidity documentation: (more info here)

The Ethereum Virtual Machine has three areas where it can store items.

The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.

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

Local variables are referenced from "storage" by default, using keyword "memory" puts them in the context of "memory", i.e. a cheap ephemeral storage.

| improve this answer | |

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.