Is this a valid way to create an array?

BlitzMax Forums/BlitzMax Programming/Is this a valid way to create an array?

Strict

Local atest:Float[,,]

atest=New Float[3,4,5]

Graphics 400,300,0
While Not KeyHit(key_escape)
	Cls
	DrawText atest[2,3,4],0,0
	Flip
Wend


I saw it in someone else's code but I didn't think you could create a multi-dimensional array like that, but I could be wrong because it seems to work...

I thought you had to initialise each element in a loop or it wouldn't work.

I think you should only initialise elements not based on basics (string,int,float...).

They get basic values (int = 0, float = 0.00... string = "") so no need to init them. Own types have a NULL so they need it.

If they don't get initialised some variables may contain garbage but valid for their borders (language dictionary for bytes,ints,doubles,...).


bye
MB

No - I think this is OK. Hey if it works, right? The thing with initialising in loops is when people are doing C-ish arrays of array pointers, rather than the multi-dimensional array you have here.

initialize: afaik this is only true for custom objects, not for int/float/double/byte/string/etc. as they're built-in objects.

As for the rest: I'd say, just do it, and if bmax complains then it's not legal to do so.., simple enough :P

edit: well then, 3 reactions in 2 minutes stating the same.. ^_^
edit2: 4 reactions in 3 minutes stating the same.. ^_^

I could be wrong because it seems to work

It works, but dreamora says it is very slow, and that your code will be quicker if you set each field individually.

I have not benchmarked this myself tho.

OK thanks all. For some reason one of my framework customers is getting a "list index out of bounds" on that array code in one of their projects - ah just spotted the bug (in his code which is different from that above) I think!

Brucey: In that case it won't be slow. Its a different thing to what you refer to (initializing an array with actual values like int arr[] = [1,2,3] which is slow as this internally fires slicing from what it seems and from its speed I would say three times ...). What above does is initialize a 3 dimensional array. The only way to do it dynamically and normally no problem. (beside the fact that [][][] would be sliceable while above isn't)


GA: Whats the bug?
Did he/she try to access the array Blitz3D like? ie 0-number of elements? Although the error somehow looks more like an array -> TList -> indexed access error if it is bound to an array

The bug was that they were calling the DRAW command before the initialisation and I only just noticed!

Hey Grey, if I buy your framework, you will debug all of my code for me? Cool! ;)

http://www.greyaliengames.com/framework/faq.html#support

including:

I cannot answer general programming questions not related to the framework, sorry. Use the Blitz forums for that, they are brilliant!


However, I'm nice so sometimes I help where I shouldn't ;-)