Sign in to follow this  
Followers 0

Auto-Detect Array Size

25 posts in this topic

Posted

I always get annoyed when the script crashes because of this.

Dim $array[2] = [0, 0, 0]

I think it would be better if it was possible to just do this.

Dim $array[] = [0, 0, 0]

It would allow one to add items to the array without having to change the size.

Share this post


Link to post
Share on other sites



Posted

Hi,

yes me too! Like in Java.

So long,

Mega

Share this post


Link to post
Share on other sites

Posted

I always get annoyed when the script crashes because of this.

Dim $array[2] = [0, 0, 0]

I think it would be better if it was possible to just do this.

Dim $array[] = [0, 0, 0]

It would allow one to add items to the array without having to change the size.

You really mean not giving the size as you expect the size be defined by the affectation value ...

I am not sure Jon will love this syntax but if it Java driven ...

Share this post


Link to post
Share on other sites

Posted

Actually, I used to feel the same way, but in thinking about it, I like the way it is now as it allows for better error catching. You supply a simple example and I know you're thinking of something more complex, but what happens when the complexity runs away from you and you have some crazy multi-dimensional array?

Say you have this:

Dim $array[2][3][4] = [ _
	[ _
		[ 1, 2, 3, 4 ], _
		[ 5, 6, 7, 8 ], _
		[ 9, 10, 11, 12 ] _
	], _
	[ _
		[ 13, 14, 15, 16 ], _
		[ 17, 18, 19, 20 ], _
		[ 21, 22, 23, 24 ] _
	] _
]

Confusing, no? Well maybe not too bad, but only because of the way it's spaced out.

Anyway, if I mixed up a bracket in there, or put too many items in the wrong dimension, I'm going to get an error message telling me so, and I'm going to realize right away what I did wrong, I'll realize I screwed up somewhere. If you have the array auto determine the size it's going to be then it's going to just compensate for mistakes like that and you'll be stuck wondering why your arrays don't have the right stuff in them.

Although as long as we can still manually declare array size that wouldn't bother me at all. Except when I'm trying to help someone debug their code and they have an array like my example here. :)

Share this post


Link to post
Share on other sites

Posted

I think new proposed syntax will make arrays internal code slower (dynamic checking sizes) also.

I can live with current syntax.

Share this post


Link to post
Share on other sites

Posted

I agree with Saunders. Better error checking for the human side of the script is a good thing.

Share this post


Link to post
Share on other sites

Posted

But:

- array size can be pre-checked. For example, C(++) also uses such syntax:

int array[] = { 4, 5, 6 };
char string[] = "blah";

And this is C, where everything is compiled to native machine code. No slowdown.

- I suppose everything in AutoIt is checked at runtime anyways (it's an interpreted thing as you all know).

Therefore I would find this proposed new feature kinda useful. Just a thought :)

Share this post


Link to post
Share on other sites

Posted

Like I said, if we could do both, that would suit me just fine.

Local $array1 = [ 1, 2, 3, 4 ] ; No error
Local $array2[4] = [ 5, 6, 7, 8 ] ; No error
Local $array3[4] = [ 9, 10, 11, 12, 13 ] ; Error!

That way it's left up to the user how they want their code to work. I'd chalk it up to preference, kind of like using Opt('MustDeclareVars').

Share this post


Link to post
Share on other sites

Posted

Like an Opt('MustSizeArrays')?

Share this post


Link to post
Share on other sites

Posted

Like an Opt('MustSizeArrays')?

More like Dynamic Arrays. Fixed arrays are not flexible so lack creditability and speed in some usage. Allocating memory for a fixed array maybe fine at first but is ruined once you decide to resize. Dynamic arrays are setup in knowledge that resizing may happen and can lead to better results then a fixed array can under the same conditions. Dynamic arrays are used in JScript, VBScript, C, Java, Lua... :)

Share this post


Link to post
Share on other sites

Posted (edited)

Dynamic arrays are used in JScript, VBScript, C, Java, Lua...

May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory... Edited by zatorg

Share this post


Link to post
Share on other sites

Posted

May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory...

Not supported so your question is a good correction to my mistake of adding C to my mentioned list of languages. Like your C example above, C allows you not to specify the amount of values within the array and is allocated during compile time (thus is not dynamic during runtime).

Intializing a dynamic array in VBScript for example can simply be done as below.

Dim MyArray()

or

Dim MyArray(1, 2, 3)

without any need to specify size. The Scripting Host will allocate suitable memory storage for the array (without any restrictions imposed by using fixed arrays).

The preference of using Fixed or Dynamic arrays falls down to need and performance. If an array only requires a fixed amount of values then the lower overhead of using a fixed array will boost performance. If resizing is a requirement then dynamic is suitable to use as dynamic sizes the array and preallocates memory (amongst any other optimization) to suit a ReDim of size.

:)

Share this post


Link to post
Share on other sites

Posted

Understood and fully agree... :)

Share this post


Link to post
Share on other sites

Posted

Like an Opt('MustSizeArrays')?

Shush! If the dev's see someone even hinting at another opt, they're liable to defenestrate you.

No, I just meant that it would automatically figure it out based on the way you write the code. If you supply dimensions, it uses, and restricts the array to that info. If not, it auto adjusts. So if you actually write Dim $myArray[3] you'll predefine an array with 3 items, just like we do now, which would be easier on the CPU. But if you leave out the size definitions, then it will automatically create array structure to fit the data you supply.

So in essence, if you predefine your array it will be faster than if you don't. Does that make any sense?

Share this post


Link to post
Share on other sites

Posted

May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory...

Take a look at the malloc, realloc and free functions. Dynamic storage is managed by these functions.

Share this post


Link to post
Share on other sites

Posted

I always get annoyed when the script crashes because of this.

Dim $array[2] = [0, 0, 0]

I think it would be better if it was possible to just do this.

Dim $array[] = [0, 0, 0]

It would allow one to add items to the array without having to change the size.

I am the developer who put the array aggregate initialization into AutoIt. When I was setting this up, I realized I needed to know how big the array was before I started to assign to it. Basically, by the time I was ready to read the array elements, I already had to have the array built, ready to be loaded with the data. So leaving out the dimension values was not going to work.

I did experiment with allowing an empty array dimension, and determining the size of the dimension by the size of the initialization data set, but it more than doubled the time and memory requirements to create and initialize the array.

In C (or other compiled languages), the data is recognized and the array size is determined during compiling, but the data is not actually assigned until run-time. During run-time, the array and its initializer sizes are already known, so you do not lose any time determining it, just allocate the array and assign the data. AutoIt, even when "compiled" into an EXE, is interpreted. This means that the array size would have to be determined each time and then the array could be sized and then the data could be inserted. This just takes way too long and eats gobs of memory, more than twice the array to be assigned. It also needed a lot of code to do, and we were trying to keep the entire AutoIt less than 100 KB at the time. I know it is a little bigger now, but still it is mighty small.

Share this post


Link to post
Share on other sites

Posted

All "dynamic" arrays are just reallocated every time they are resized. It doesn't matter what the source looks like, deep down, it is still a reallocation.

@Saunders: How are they supposed to throw me out of the window of a forum?

Share this post


Link to post
Share on other sites

Posted

Hey man they're the developers.. I'm sure they'll find a way. :)

On a completely separate note, when did Nutster come back?

Share this post


Link to post
Share on other sites

Posted

Hey man they're the developers.. I'm sure they'll find a way. :)

On a completely separate note, when did Nutster come back?

Last week. The project that I was working on for the last two years is finally done and I find I have free time again! :)

Share this post


Link to post
Share on other sites

Posted

All "dynamic" arrays are just reallocated every time they are resized. It doesn't matter what the source looks like, deep down, it is still a reallocation.

@Saunders: How are they supposed to throw me out of the window of a forum?

When you ReDim, the new array is created and then all the old data is copied, element by element, to the corresponding elements of the new array. Because of this, you do not want to be ReDim'ming arrays too often. Grow your arrays several elements at a time, to reduce the number of Redim calls you make. This is another part of the array system that I wrote, so I know how this works (if my memory is still operating after all this time).

Throw you out? Hmm, let's see. Are you on the banned list? :) I think I can put you there if you want.

Share this post


Link to post
Share on other sites

Posted

No thank you, I would really prefer to stay here at the forum. I don't know that I really deserve to be banned. I didn't suggest the new option, I was just asking if that was kind of what the other poster meant.

Too bad we can't have a linked-list type built in. That would be the most efficient dynamically sized array. Elements can easily be added and removed. If someone feels like adding that to the suggestions forum, feel free. I've learned not to create suggestions.

Share this post


Link to post
Share on other sites

Posted (edited)

Too bad we can't have a linked-list type built in. That would be the most efficient dynamically sized array. Elements can easily be added and removed. If someone feels like adding that to the suggestions forum, feel free. I've learned not to create suggestions.

Then it wouldn't be an array.

Arrays are good because they are continuous regions of memory so if you need to work with elements n to m you'll need to hover through (m-n) * sizeof( OneElement ) bytes of memory (fast memory access is one of the fundamental concepts of arrays). If one uses a linked list and needs to access the nth element, one will need to at first access (n-1) elements accessing their fields which hold pointers to the next element. And if all the elements are scattered through memory (and they are as long as it is Win32 heap), it makes even slower to access the nth element.

In short, linked lists are a lot slower than huge chunks of memory in one place. They are that slow that even realloc()'ing memory for arrays is probably overall faster than using linked lists.

My 3.14th of a cent :)

Edited by zatorg

Share this post


Link to post
Share on other sites

Posted (edited)

Take a look at the malloc, realloc and free functions. Dynamic storage is managed by these functions.

Right, I know these, thanks :) I was inclining about dynamic arrays in C. I thought MHz meant that dynamic arrays is one of fundamental types in C (and it is not) but he responded that he didn't mean that.

Anyway, I wasn't asking about how dynamic arrays could be implemented. Again, thanks anyway :P

I've learned not to create suggestions.

Oh come on Richard, I'm sure Nutster was joking! :)

Edit: damn you grammar!

Edited by zatorg

Share this post


Link to post
Share on other sites

Posted

No thank you, I would really prefer to stay here at the forum. I don't know that I really deserve to be banned. I didn't suggest the new option, I was just asking if that was kind of what the other poster meant.

Too bad we can't have a linked-list type built in. That would be the most efficient dynamically sized array. Elements can easily be added and removed. If someone feels like adding that to the suggestions forum, feel free. I've learned not to create suggestions.

Sense of humor bypass! He was clearly joking :) (he doesn't have Mod powers either :) )

Share this post


Link to post
Share on other sites

Posted

Too bad we can't have a linked-list type built in. That would be the most efficient dynamically sized array. Elements can easily be added and removed. If someone feels like adding that to the suggestions forum, feel free. I've learned not to create suggestions.

I think this can be done with DllStructCreate:

- make struct with these items: Data, Prev, Next - Prev/Next will be ptr type

- In Next/Prv is stored DllStructGetPtr() - pointer to previous/next sctructure

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0




  • Recently Browsing   0 members

    No registered users viewing this page.