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.
memory
. So my question is when would I use fixed/dynamic arrays for arrays stored inmemory
.