HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Arrays question

12-23-2003, 08:40 AM#1
Kolibri
So arrays have indexes 0,1,...,size-1

That is an array of size n can use the indexes 0,1,2,...,n-1

However I could have sworn that I have a bunch of arrays with length 5 where I use the index 5 (array[5]), so how come that works?

I think that is kind of strange.
12-23-2003, 08:55 AM#2
Newhydra
Because that number is rather meaningless...you can try to store something in array[7] with a length 5 array, most of the time it'll work, sometimes it won't (depending on which variable type you're storing)
12-23-2003, 09:08 AM#3
Kolibri
Hmm, I'd better increase the length of most of my arrays by one.
12-23-2003, 06:13 PM#4
Zechnophobe
Blizzard is too nice: When you make an array with size n, you are ACTUALLY making an array of size N+1. THey did this so that people not well versed with arrays could still use them without worry about spill over.

Secondly: I'm pretty sure that if you overstep the bounds of your initial array size, WE will just dynamically expand your array to be large enough. If you know anything about dynamic array expansion, you know its not generally a good thing to be doing, so set that initial size of the array to how big you really plan on it being.
12-23-2003, 08:09 PM#5
Ligature
As far as I can tell, the arrays in the GUI are completely resizeable - that is, if I have an array ThisArray of size X, and I "Set ThisArray(X+12) = Y" I have just created a new variable at index X + 12 in ThisArray. It's as simple as that. So really, if you'll pardon the pun, size doesn't matter!
12-23-2003, 10:22 PM#6
Zechnophobe
Like I said, the arrays don't just magically resize themselves, or they wouldn't even bother asking for an initial size. It has been noted that the size of the map is actually 1 size higher then what you tell it to, so that the index with a value equal to the array size still works.

When you use an out of bounds index, what they are almost assuredly doing, is copying all of the information from the array into a new array with a size large enough to encompass the out of bounds index.

That means if you make a size 8 array, and attempt to access index 9, then 10, then 11, you are making not just 3 data checks, but more like 30+. You can be a slacker and not worry about that, but every little bit counts, y'know.

In contrast, if you had just made your array absurdley large (100) before accessing index 9 then 10 then 11, you would take up a very small amount of memory instead.
12-23-2003, 10:25 PM#7
weaaddar
It doesn't matter what indice you place it in. The size lists the amount of unique indicies the array can have.
12-23-2003, 11:16 PM#8
Peppar
The size you input in the variable section only means what range in the array will be initialized to the value you choose.

You can use up to 8192 entries in an array, no matter what size you specify in the variable section.

So if you create an integer array my_arr with size 100 and the value 123, my_arr[0] to my_arr[99] will be set to 123, but if you want you can use my_arr[100] to my_arr[8191] without Warcraft complaining.
12-24-2003, 07:45 AM#9
Zechnophobe
Quote:
Originally posted by Peppar
The size you input in the variable section only means what range in the array will be initialized to the value you choose.

You can use up to 8192 entries in an array, no matter what size you specify in the variable section.

So if you create an integer array my_arr with size 100 and the value 123, my_arr[0] to my_arr[99] will be set to 123, but if you want you can use my_arr[100] to my_arr[8191] without Warcraft complaining.


Yes this is true, but when you go with the 'out of bounds' index, it resizes your array which It won't handle well if you do it a lot. If you really think you'll need 8k array indices, make the array that big to start with, dont' require a possibility of 8000 resizes in 2 seconds. Honestly, its just easier on the CPU that way.
12-24-2003, 11:08 AM#10
Peppar
I don't know how arrays are stored in-game, but if they are stored as vectors then you'd be better off setting my_arr[0] = 0 and my_arr[Size - 1] = 0. Then the game would only have to resize the array once.

If you look at the compiled Jass, you will see that the initialization of the vectors is just a loop where it sets all the array entries from 0 to Size - 1 to whatever value you want, which would make the game resize the array just as many times as it would if you were to use the array without initialization (Although it would put the calcuations at map init instead of runtime, which is good.)
12-24-2003, 01:39 PM#11
Extrarius
Looking in the generated jass for a map I made, it seems that the tft editor initializes [0] to [n] in the InitGlobals function. The ThreadsStopped var is of size 3:
set i = 0
loop
exitwhen (i > 3)
set udg_ThreadsStopped[i] = false
set i = i + 1
endloop
It would be nice if the editor would combine the 50 loops like that when the exit condition is the same. Could have 73 lines of code instead of 156 on my last map if it would just combine the loops.
12-25-2003, 01:27 AM#12
Kolibri
Thanks, guess I don't have to increase the length of my arrays. :D