Quick Question about Arrays
Blitz3D Forums/Blitz3D Beginners Area/Quick Question about Arrays
| ||
Dear all, Another really simple question, not too important but I would like to know the answer for reference. When coding arrays does it have a big impact on the code if you use 'huge' arrays rather than compact ones even if the majority of the array is empty? For example.......If I have 10 names that I want to store in an array then the simplest way would be the first example below. Create an array with 10 points. However the second example below stores the same amount of data but using an array with a size of 10000. DIM Name$(10) For a = 1 to 10 Name$(a) = "Whatever" Next or DIM NAME$(10000) For a = 1 to 10000 Step 1000 Name$(a) = "Whatever" Next How much impact would the second way have on the code? Would it take up a lot more memory or would the increase in memory be very very minimal and thus not a problem? Finally how much more space to multi dimensional arrays take than normal arrays? For example how much less memory would a Dim test(500) use compared to a Dim test (500,10) And how many dimensions can you go to? The Manual just says - Dim array_name(index1[,index2][,index3][,...]) Which seems to indicate you can create as many dimensions inside the array as you like.......but in my experience I have never seen any that has more than 3 index levels. Again I apologise for the basic questions, and also if I have misunderstood the most basic of functions. |
| ||
Each empty string takes up either 4 or 8 bytes i would think: 10,000 x 4 bytes = 40,000 bytes = ~40Kb 10,000 x 8 bytes = 80,000 bytes = ~80Kb Multi dimensional arrays can increase size rapidly: (100,100) x 4 = 100 x 100 x 4 = 40,000 (100,100,100) x 4 = 100 x 100 x 100 x 4 = 4,000,000 (100,100,100,100) x 4 = 100 x 100 x 100 x 100 x 4 = 400,000,000 I don't think there is a (noticeable) limit on how many dimensions you can have. |
| ||
As Beaker says, I would think each unused array element (for a string array) would use 4 bytes because, internally, I expect each element is a pointer to the string (char * in C). The basic rule for arrays is to only use the minimum amount of elements you need - anything more is waste and should be avoided simply on the grounds of good programming practice. :) Having said that, there are occasions where "padding" an array makes sense for practical reasons. For example, I recently needed an array that held a value for every printable character in the ASCII set - this excluded characters 0 to 31 as they're control characters. I still dimensioned my array as char%(255) though, so that I could index it using the ASCII code of any character. |
| ||
Look into "Sparse Arrays" if you find you're wasting a lot of memory. |