What is the difference between arr_0 and arr_1 Both are initialized to have 4 items (all initialized to zero). Both seem to support push and pop operations as well

contract Test {   
     uint8[] public arr_0 = new uint8[](4);
     uint8[] public arr_1 = [0,0,0,0];
     uint8[] public arr_2;
     function pushItem(uint8 x) public {
     function popItem() public {
     function g() public {
         uint8[] memory x = new uint8[](2);
         arr_0 = x;
         arr_1 = x;
         arr_2 = x;
        //arr_0, arr_1, arr_2 now have 2 items

The solidity version I am referring to is 0.8.13

1 Answer 1


With this statement uint8[] public arr_1 = [0,0,0,0]; you're creating an array witch its length depends how many values you put into square brackets. In this case, the array has a length 4 (because you put until third index the value '0'). Ex.:

uint8[] public arr_1 = [10,20,30,40,50,60];
Length: 6

On the contrary, in this statement uint8[] public arr_0 = new uint8[](4);, you're defining an array that it has length 4 (in this case) without specificy the single values into array index (like previous statement).

Both cases you can add the element into array with push() method.

  • Does that mean both are two different syntaxes for the exact same thing? What about uint8[] public arr_1; AND uint8[] public arr_0 = new uint8[](0); Now, I am not even specifying single digits for the former case. For all practical purposes, both seem to function exactly the same. Mar 31 at 5:47
  • Not really, these syntaxes is used for declare or initialized an array. When you wrote this: uint8[] public arr_1; you're defining a dynamic array without specificy a its length. While when you wrote this: uint8[] public arr_0 = new uint8[](0); you're specifing the length between curve brackests (in this case is wrong because you cannot create an array with 0 length)
    – Kerry99
    Mar 31 at 12:19
  • "You cannot create an array with 0 length" Are you sure about this? I just tried it with v 0.8.13 and it compiled AND executed successfully without any problem. Also, there is a small difference in gas costs (for push, pop, and getter operations) between arr_0 and arr_1 Mar 31 at 12:45
  • I got confused! You can declare an array with zero length. For second affirmation, it's correct but depends from what you need to do (add a value to array, remove a value from a specific array index or return value from a array index)
    – Kerry99
    Mar 31 at 13:47
  • I want to believe that both are same. But there is a difference in gas costs for similar operations. For eg. pushing the first element to arr_0 is costlier than pushing the first element to arr_1. similarly. Similarly pushing an element (at any index) to arr_0 is costlier than pushing an elment to arr_1 at the same index. The cost differences are not too big, but they are not zero either. This make me think there IS some diffrence, and when should we prefer one over the other Mar 31 at 14:03

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.