Author Topic: Static vs dynamic arrays  (Read 5234 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Static vs dynamic arrays
« Reply #15 on: October 31, 2020, 01:47:23 am »
Eep.  Er.  Thought I got an error message saying I could not do that.
Now I'm really confused. 


My work is done! ;-))

Yeah REDIM what do ya mean? I haven't DIM it yet! (when you start a Dynamic array.)
« Last Edit: October 31, 2020, 01:49:57 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Static vs dynamic arrays
« Reply #16 on: October 31, 2020, 02:15:57 am »
Eep.  Er.  Thought I got an error message saying I could not do that.
Now I'm really confused. 

Got it.  Sorry I wasn't clear.  It's a SHARED array.  That's why it isn't
legit to REDIM it in a SUB.  Unless I leave off the SHARED.  Then what
happens?  I find out...

Works fine, which suprised me.  Other SUBs can still access it.  Duh.
Sorry.  I give this entire matter a big rethink.  So.....ERASE is really
stupid, REDIM is the better way?  ERASE is just for QB4.5 compatibility?

Man you are doing my trick adding edits one after the other...

Serves me right I guess.

Sorry about the SHARED adding more mud to the murkyness.

ERASE is for Static arrays that are started with keyword DIM.

Use REDIM to start a Dynamic array (one you can change dimensions but not Type) AND use it to 'erase' ie zero it out or reset strings to "".

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Static vs dynamic arrays
« Reply #17 on: October 31, 2020, 02:22:35 am »
The only danger in using a REDIM / Dynamic array is if you are doing MEM tricks according to Steve.

Otherwise if DIM / Static arrays aren't any faster than Dynamic then DIM / for Static arrays / ERASE become dinosaur too I guess.
« Last Edit: October 31, 2020, 02:37:20 pm by bplus »

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Static vs dynamic arrays
« Reply #18 on: October 31, 2020, 03:53:30 am »
This entire post was a lack of knowledge on my part, thinking that redimensioning a
shared array would be illegal, or change the common status, or somehow cause a problem. 
It doesn't, and the redim does zero it, unless PRESERVE is used.

Goombye ERASE command, I'm glad to 'ave known ye.


« Last Edit: October 31, 2020, 03:55:40 am by Richard Frost »
It works better if you plug it in.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Static vs dynamic arrays
« Reply #19 on: October 31, 2020, 09:46:14 am »
Richard - bplus : Thanks for shining a light on this one.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Static vs dynamic arrays
« Reply #20 on: October 31, 2020, 09:47:02 am »
Eep.  Er.  Thought I got an error message saying I could not do that.
Now I'm really confused. 

Got it.  Sorry I wasn't clear.  It's a SHARED array.  That's why it isn't
legit to REDIM it in a SUB.  Unless I leave off the SHARED.  Then what
happens?  I find out...

Works fine, which suprised me.  Other SUBs can still access it.  Duh.
Sorry.  I give this entire matter a big rethink.  So.....ERASE is really
stupid, REDIM is the better way?  ERASE is just for QB4.5 compatibility?

I also tend to go this route:

REDIM Array(1234) AS INTEGER
DIM M AS _MEM: M = _MEM(Array())
_MEMFILL M, M.OFFSET, M.SIZE, 0 AS _BYTE 

MEMFILL works as good as ERASE, it’s often faster, and you can customize what you want to fill the default with.  (In the above case, it’s CHR$(0) as my fill, but in some cases, CHR$(32) might be more suitable, or even a RGBA value, for a background image refresh.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Static vs dynamic arrays
« Reply #21 on: October 31, 2020, 11:24:20 am »
Richard - bplus : Thanks for shining a light on this one.

Thank Luke for shining the light for me, glad to share the light though :)

And Steve just showed a good use of MEM stuff with a REDIM / dynamic array, so certainly not bad to use _MEM stuff with these arrays just use caution.

Looks safe right after a REDIM or if you know you haven't used REDIM since last MEM check.
« Last Edit: October 31, 2020, 11:41:45 am by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Static vs dynamic arrays
« Reply #22 on: October 31, 2020, 01:29:15 pm »
I'll contribute a little too. I use static arrays where I know that the number of elements in an array cannot change. For example, for icons. The program has a fixed number of icons, there is no need to store them in a dynamic field. Likewise, these fields contain only a handle, QB64 stores its own data for the images somewhere ... and sends us a handle. That's enough.
In order for a dynamic field to be dynamically oversized, it does not have to be SHARED, it is enough if is for the function passed as a parameter. I think this method is more suitable if you are working on a .BM function that will be used in many other programs in the future.

And now the question. I will have a dynamic field. However, I don't want to still write FUNCTION EXAMPLE (ARR () as something) as a function parameter, but I want to pass a handle there. A reference to the address of the field in memory. I'm going to do some experiments with it, because I think _MEM offers this possibility. The notation of the function will then be, I think, clearer, as the commands in QB64 do it.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Static vs dynamic arrays
« Reply #23 on: October 31, 2020, 10:42:51 pm »
It never would have occurred to me to fill an array with a color, and I forgot about MEMFILL.  I'm still
new to using memblocks, and so far have only used them to shift arrays one element to the left.



It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Static vs dynamic arrays
« Reply #24 on: November 01, 2020, 01:10:47 am »
It never would have occurred to me to fill an array with a color, and I forgot about MEMFILL.  I'm still
new to using memblocks, and so far have only used them to shift arrays one element to the left.

Yeah, I've got MEM code to copy an array which I am sure will be handy one of these days.