Help with Arrays
BlitzMax Forums/BlitzMax Beginners Area/Help with Arrays
| ||
Hi everyone, I am trying to set an array of my user-defined type but can't make it work. The code below generates an error. Any help? Thanks! SuperStrict Global p_arr:mypoint[] Type mypoint Field x:Float Field y:Float End Type p_arr = New mypoint[10] p_arr[0].x=10 |
| ||
Works for me, but the code does look odd... [Edit: just ran it in debug mode and it crashes, release mode worked!?!?] What error are you getting? [Edit: Dont worry] I would do it something like this: SuperStrict Global p_arr:mypoint[10] Type mypoint Field x:Float Field y:Float End Type For Local i% = 0 To 9 p_arr[i] = New mypoint p_arr[i].x=10 Next p_arr[5].x=20 For Local i% = 0 To 9 Print i + " " + p_arr[i].x Next Or: SuperStrict Global p_arr:mypoint[] Type mypoint Field x:Float Field y:Float End Type p_arr = New mypoint[10] For Local i% = 0 To 9 p_arr[i] = New mypoint p_arr[i].x=10 Next p_arr[5].x=20 For Local i% = 0 To 9 Print i + " " + p_arr[i].x Next |
| ||
Hey thanks, So we have to initialize each element of the array separately? I thought that when you initialize the array you would have all the elements initialized. thanks for your help! |
| ||
When you define a single mypoint variable it must still be created with New. If you had ten such variables, named p_arr0, p_arr1... p_arr9 they would all have to be created with New before being used. Concepually, the array is just ten new variables named p_arr[0], p_arr[1]... p_arr[9]. The corresponding objects must be created with New before they can be used. |
| ||
You might not realise it, but an array is an object too! New mypoint[10] creates a new array object that can store 10 mypoints, but it does not yet contain any actual mypoints. Like the code above, you need to initialize each slot manually. A general way that will work for any size of the array would be:For Local i:Int = 0 Until p_arr.Length p_arr[i] = New mypoint Next |
| ||
Does any know why the code worked in release mode, but not in debug mode? (Using BMax 1.41) |
| ||
It worked only in the sense that the failure went undetected. |
| ||
When I tested all the objects had the value 10 in them, run this bit of code in Release and Debug mode:SuperStrict Global p_arr:mypoint[] Type mypoint Field x:Float Field y:Float End Type p_arr = New mypoint[10] p_arr[0].x=10 For Local i% = 0 To p_arr.Length - 1 Print i + " " + p_arr[i].x Next |
| ||
Does any know why the code worked in release mode, but not in debug mode? (Using BMax 1.41) Debug mode adds extra checks that release mode doesn't. I believe that in debug mode, the array is automatically filled with the Null object and trying to access any data will cause an error, but in release mode, the array is left with uninitialized data for speed, and so when you write to a field, it will be writing to a random area of memory. Release mode will also skip array bounds making the code below work. 'This code will "work" in Release mode, but Not debug Local Array:Int[10] Array[10] = 10 Print Array[10] |
| ||
Hey guys, I just wanted to say thanks for your help. I initialized every element of the array in a loop and worked perfectly. |