4

I was looking at the documentation and was looking at arrays stored in memory. Now, the documentation said that arrays stored in memory cannot change their size using .push() or .pop() functions. This makes sense. What doesn't make sense is the difference between these two:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract Arrays {

    function test() public pure returns (uint[3] memory, uint[] memory) {
        uint[3] memory testArray;
        testArray[0] = 3;
        testArray[1] = 4;
        testArray[2] = 5;
        
        uint[] memory testArray2 = new uint[](3);
        testArray2[0] = 13;
        testArray2[1] = 14;
        testArray2[2] = 15;
        
        return (testArray, testArray2);
    }
}

I understand that the uint[] memory testArray2 = new uint[](3); is a dynamic array whereas the uint[3] memory testArray; is a static array. But what's the difference? Are they the exact same thing since they are both of fixed size?

If they are different: when would I use each? What are the pros/cons to each one?

5
  • 1
    You should use static when you know when the data being stored is fixed. For example, to store weekdays or month names you should use static array. Dynamic is when you are storing names or something whose data size are not fixed. Since the size of static array is fixed you cannot use push or pop. Sep 23, 2021 at 3:52
  • Thank you @AbhishekSinha ! But, when it's in memory, you can't use push or pop anyway. So what's the difference then? Thanks! :)
    – tsnakejake
    Sep 23, 2021 at 3:58
  • Use static array when you know you don't have to add/remove any element. If you have to you must use dynamic array. In dynamic arrays you can use push and pop to add remove items. Sep 23, 2021 at 9:01
  • @AbhishekSinha Yes, but you can't use push and pop for arrays stored in memory. So my question is when would I use fixed/dynamic arrays for arrays stored in memory.
    – tsnakejake
    Sep 23, 2021 at 14:00
  • 1
    Okk..yes, in your case both are same. Since you are using memory keyword to store the data, the data will be flushed after the function execution is over. So here both the methods are equivalent. Sep 23, 2021 at 15:09

1 Answer 1

2

This is a common problem that beginner developers are facing. The most important thing is this is related to almost all the programming languages. So, I'm giving this answer as a general answer for all the programming languages. Let's go through them (array types) one by one.

STATIC ARRAYS

Static arrays have fixed sizes. In another way, a fixed amount of elements. It should be mentioned when the array is initialized/declared. After that, we can not add more elements to that array.

Eg: If the size is 5, we can add a maximum of 5 elements. Not more than 5.

DYNAMIC ARRAYS

Dynamic arrays AKA ArrayLists are arrays which don't have a fixed size. In another way, we can say they don't have a fixed amount of elements. We can allocate any amount of elements that we wish. No limits. We can do it when we initialize the dynamic array or we can do it later from time to time by adding elements one by one. The basic idea is that dynamic arrays haven't fixed size, we can add any amount of elements to dynamic arrays as we wish.

DYNAMICALLY ALLOCATED ARRAYS / DYNAMIC MEMORY ARRAYS

Dynamically allocated arrays are almost similar to dynamic arrays. The main visible difference is when they are declared we have to give a size to them. But after that, if we want to exceed that size, we can do it without any problems. There will be no errors.

Eg: There is a dynamically allocated array with a size of 3. That means we are supposed to insert 3 elements into that array. But if we want to add more than 3 elements to that array, without any hesitation we can do that. (Keep in your mind that different languages have different ways to do it.)

Importance of Dynamically Allocated Arrays

In dynamically allocated arrays, we don't have to assign values to indexes in order. Usually, in arrays, we have to assign values like the below ones if we add elements one by one.

array[0] = 1;
array[1] = 2;
array[2] = 3;

First, we should assign a value to the 0th index. Next, we should go in order like 1, 2, 3, ...

But, in dynamically allocated arrays, we can assign values as we wish. Like the below ones.

array[1] = 2;
array[2] = 3;
array[0] = 1;

You can see that I have assigned a value to the 1st index at first. Next, to the 2nd index. Finally, to the 0th index. No order at all. If you want to do this kind of thing, the dynamically allocated array is the best option.

P.S. In solidity, you don't have an option to use push or pop to dynamically allocated memory arrays. When comes today (Sep 20, 2022), the latest Solidity version is 0.8.17 and still, we can not use pop or push with memory arrays. They may have a special reason not to allow that. If anyone knows it please comment down the reason. Thanks in advance.

But, in Solidity this (below one) is totally valid.

uint256[] memory arr = new uint256[](3); // size of this dynamically allocated memory array is 3.

arr = [1, 2, 3, 4, 5, 6]; // but here I have assigned 6 values. It's totally acceptable since this is a dynamically allocated array.

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.