I have a storage array and I able to use push method. But, in another part of my code, I have a memory array and I can't do that. What's the reason? Doesn't exist push method in memory arrays?

        Individual[] memory newPop = new Individual[](popSize);
        for(uint i = 0; i < popSize; i++){
            uint index1;
            uint index2;
            (index1, index2) = getIndex();
            Individual newIndiv = crossover(index1, index2);

And the error:

Error using push method


Keywords push and pop are applicable only for dynamic arrays.

Dynamic arrays can be declared only in storage (i.e., in global scope) AND with no predefined size.

For example: uint256[] public arr.

Aside from that, you've already allocated the local array newPop to the desired size, so why would you want to hypothetically use push and increase that size?

You should simply change this:


To this:

newPop[i] = newIndiv;
  • Working, thanks a lot! I'm just starting with Solidity – Joncarre Oct 26 '20 at 13:09
  • You're welcome. – goodvibration Oct 26 '20 at 13:09
  • By the way, is it possible to debug step by step in Solidity (like in Java, C++, etc.)? – Joncarre Oct 26 '20 at 16:06
  • @Joncarre: That's an IDE question, not a Solidity question. It is possible via Remix, Buidler, and perhaps a couple of other tools. You can also do it in the "old fashion" way and add revert or require statements in your code, in order to find the exact point of failure. – goodvibration Oct 26 '20 at 16:17

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.