0

I get an error when trying to initialize array inside a struct of an array and I find this post: https://github.com/ethereum/solidity/issues/4115 .

It seems that I have to work around by increasing the size of the outer array by 1 and fill the value by hand. But does it guarantees that the newly appeared elements are initialized properly to the default value of their types? And, without explicit memory reservation which is often required in other similar languages, will the array overlap with other data?

1
  • Please share your code. Apr 14, 2019 at 15:15

1 Answer 1

1

And, without explicit memory reservation which is often required is other similar languages, will the array overlap with other data?

It's practically impossible for your array overlapping other data in your smart-contract. You can read more here as why.

When you expand a dynamically sized array by increasing its length, it will allocate space for the type of the object your array holds and the value(s) will be already initialized to 0.

You can try it out with this. Open it in remix and increase the array's length at will and use the getter for the array.

pragma solidity ^0.5.0;


contract Contract {

    struct Everything {
        uint a;
        bytes32 b;
        bool c;
        address d;
    }

    Everything[] public my_array;

    function expand() public {
        my_array.length++;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.