Since the new version of Solidity removes the return value of the push operation to arrays (Before the push operation returns the new length of the array), my question is if it is safe to call the length (length - 1) method after push to get the index of the new added element?

2 Answers 2


This is the answer to the question asked in the comment section of the first answer.

Short answer: Unlike C or C++ there is no concurrency or Multi-thread programming (parallelism) in Blockchain and Ethereum smart contract. The blockchain itself is effectively single-threaded. So you can't have a problem where same modifying actions occur at the same time.

Answer with more details: Every change you make in a smart contract is a transaction. And a transaction must be included inside a block in order to get executed. Lets say we have 2 functions and they both add an integer in an array. Function A and function B like this:

pragma solidity ^0.6.8;

contract Test {

    uint256[] public array = [0,1,2];

    function A(uint256 input )public {
            //a bunch of code in here

    function B(uint256 input )public {
            //a bunch of code in here

We have an array initialized with 3 elements, so the array.length is 3 at the beginnig. Lets assume 2 people click on these functions simultaneously. One clicks on "run A" function button and the other clicks on "run B". Now these 2 transaction will be added in the Mempool at same time, but won't get executed till someone picks it up and include them in their block. The block validation process will take something around 15 seconds. There are 2 possibilities

  1. These 2 transactions both get into one block: Now when the block gets validated they both get executed in the same time. so if you call the array.length before the block validation, you will get 3 as the result, and if you call it after the validation you will get 5. and you can't call array.length in between the execution of these functions because they both get executed at the exact same time.
  2. Transactions get into separate blocks: In this example one of those 2 people who clicked on the function button must wait a little more .if you wait 15 seconds for the first transaction to get executed, you will get 4 for the array.length call. if you wait for another 15 seconds, you will get 5 . So there's no such "Wrong answer" for the array.length call. You just have to wait more.

If you are showing the array.length in your web page, you can refresh the page after a transaction got executed.

PS: There are some people working on papers titled as "Concurrency or parallelism in smart contracts". They are proposing a solution to make blockchains with concurrency and parallelism support for the smart contracts being written on them.

Hope it helps. kind regards.


Yes that's actually the only safe and reasonable way to get the index of the last element.

doing this:

  • But can I be sure that the last element added is Array.length - 1. Could it be possible that the function is executed simultaneously by two threads and both push at the same time? When I want to get the index in the next line with Array.length - 1, one call get the wrong index. Or is this not possible in smart contracts?
    – wero026
    Commented Jun 9, 2020 at 9:27
  • That is a different question i will answer this in a minute.
    – Masoud jt
    Commented Jun 9, 2020 at 11:25

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.