Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Auto-Detect Array Size


  • Please log in to reply
24 replies to this topic

#1 MISIIM

MISIIM

    Wayfarer

  • Active Members
  • Pip
  • 74 posts

Posted 01 September 2007 - 11:01 AM

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.







#2 Xenobiologist

Xenobiologist

    Xx Code~Mega xX

  • MVPs
  • 4,821 posts

Posted 01 September 2007 - 11:04 AM

Hi,

yes me too! Like in Java.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files *new

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times


#3 jpm

jpm

    a Real GUI/debug lover

  • Developers
  • 9,550 posts

Posted 01 September 2007 - 03:34 PM

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 ...

#4 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 02 September 2007 - 02:18 AM

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. :)

#5 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,557 posts

Posted 02 September 2007 - 12:15 PM

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

#6 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,187 posts

Posted 02 September 2007 - 01:32 PM

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

#7 zatorg

zatorg

    Wayfarer

  • Active Members
  • Pip
  • 79 posts

Posted 04 September 2007 - 12:10 PM

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 :)

#8 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 07 September 2007 - 10:44 PM

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').

#9 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,187 posts

Posted 08 September 2007 - 12:29 PM

Like an Opt('MustSizeArrays')?

#10 MHz

MHz

    Just simple

  • MVPs
  • 5,660 posts

Posted 08 September 2007 - 01:21 PM

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... :)

#11 zatorg

zatorg

    Wayfarer

  • Active Members
  • Pip
  • 79 posts

Posted 08 September 2007 - 01:30 PM

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, 08 September 2007 - 01:32 PM.


#12 MHz

MHz

    Just simple

  • MVPs
  • 5,660 posts

Posted 08 September 2007 - 04:22 PM

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.

:)

#13 zatorg

zatorg

    Wayfarer

  • Active Members
  • Pip
  • 79 posts

Posted 08 September 2007 - 04:27 PM

Understood and fully agree... :)

#14 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 09 September 2007 - 09:47 AM

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?

#15 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 14 September 2007 - 06:29 PM

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.

David Nuttall

Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius
AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...


#16 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 14 September 2007 - 08:36 PM

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.

David Nuttall

Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius
AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...


#17 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,187 posts

Posted 15 September 2007 - 04:38 AM

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?

#18 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 15 September 2007 - 06:54 AM

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

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

#19 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 19 September 2007 - 04:47 PM

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! :)

David Nuttall

Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius
AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...


#20 Nutster

Nutster

    Developer at Large

  • Developers
  • 1,450 posts

Posted 19 September 2007 - 05:14 PM

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.

David Nuttall

Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius
AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users