Sample Header Ad - 728x90

What happens if I start a bash array with a big index?

2 votes
1 answer
315 views
I was trying to create a bash "multidimensional" array, I saw the ideas on using associative arrays, but I thought the simplest way to do it would be the following:
for i in 0 1 2
do 
    for j in 0 1 2
    do
        a[$i$j]="something"
    done
done
It is easy to set and get values, but the jumps the indexes might make it terrible for arrays a bit bigger if bash allocates space for the elements from index 00 to 22 sequentially (I mean allocating positions {0,1,2,3,4,...,21,22}), instead of just the elements which were actually set: {00,01,02,10,11,...,21,22}. This made me wonder, what happens when we start a bash array with an index 'n'? Does it allocate enough space for indexes 0 to n, or does it allocate the nth element sort of individually?
Asked by Wellington de Almeida Silva (23 rep)
Aug 11, 2023, 03:31 PM
Last activity: Aug 11, 2023, 08:29 PM