Why does a for loop overflows when not empty?

Hello! I have the following function:

mapping(Contract=> address) public contractsToCreator;
Contract[] public ContractsArray;

//Retrieves in memory an array of the contracts created by a certain address
function getMyContracts() public view returns (Contract[] memory) {
    Contract[] memory contractsByAddress;
    uint counter = 0;
    for (uint i=0; i<ContractsArray.length; i++) {
      if(contractsToCreator[ContractsArray[i]] == msg.sender) {
        contractsByAddress[counter] = ContractsArray[i];
    return contractsByAddress;

This throws * 0:address: when the sender has not created any contract, but once it does it throws:

"error": "Failed to decode output: Error: overflow (fault="overflow", operation="toNumber", value="35408467139433450592217433187231851964531694900788300625387963629091585785856", code=NUMERIC_FAULT, version=bignumber/5.5.0)"

you need to initialize contractsByAddress with new Contract[](size)

Hello @helio.rosa! Yes it works, I actually initialized it with new Contract[](ContractsArray.length) Thank you!

hello I have similiar issue, can someone help? returns same error