Author Topic: REDIM _PRESERVE bug  (Read 6872 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Re: REDIM _PRESERVE bug
« Reply #15 on: March 29, 2020, 09:16:03 am »
Oh, that was a fun thread.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #16 on: March 29, 2020, 10:55:31 am »
This is an interesting problem. Out of curiosity I've experimented with how one could rearrange the array data without the need for a temporary array (that is, after the memory region has been extended in size). I got a solution that works okay if you're expanding dimensions, but I've not been able to get anything that works for when you increase some dimensions and decrease others (I'm primarily interested in solutions for any number of dimensions, though I've been playing with 3 mainly).

That said, it's perhaps all just academic because for arrays that are large enough to matter realloc() probably is moving the data anyway. Maybe. I don't really know.

I know where in qb64.bas to modify it, it's just the way the array code is generated is an absolute pain to work with.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #17 on: March 29, 2020, 11:17:52 am »
Well in other Basics where the is no REDIM _PRESERVE the workaround is to over dimension the array and use your own pointers to track upper, and maybe even lower, bounds. Back in Basic's day, you didn't want to waste the space but now days it's much more available.

« Last Edit: March 29, 2020, 11:21:34 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #18 on: March 29, 2020, 12:24:00 pm »
So this could be a stupid question but...is it possible to make a Random Array? Would a Random Array be similar to a Redim _Preserve Array?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #19 on: March 29, 2020, 12:34:31 pm »
So this could be a stupid question but...is it possible to make a Random Array? Would a Random Array be similar to a Redim _Preserve Array?

You mean like Random access for a file?

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #20 on: March 29, 2020, 12:48:27 pm »
After watching Steve's video on REDIM _PRESERVE I came away with an irrational fear of ever using it!  If anybody can come up with the Greek for REDIM _PRESERVE and stick -phobia after it, there'd be a completely new psychological condition to be added to psychologists' lists.
« Last Edit: March 29, 2020, 12:58:17 pm by Qwerkey »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: REDIM _PRESERVE bug
« Reply #21 on: March 29, 2020, 12:56:16 pm »
After watching Steve's video on REDIM _PRESERVE I came away with an irrational fear of ever using it!  If anybody can come with the Greek for REDIM _PRESERVE and stick -phobia after it, there'd be a completely new psychological condition to be added to psychologists' lists.

Represervaphobia?  Sounds like a winner to me!
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: REDIM _PRESERVE bug
« Reply #22 on: March 29, 2020, 01:07:49 pm »
Steve what did you say!?!? LOL

@Dimster
Do you know there are 2 kinds of arrays, Static and Dynamic are official names, and Statics are started with DIM and or STATIC and Dynamic are started with REDIM or $DYNAMIC metacommand (I never fooled with).

Actually Random access for an array sounds quite interesting, too bad the compiler has to reserve space in advance of using an array outside it's current boundary which is what REDIM _PRESERVE does. How does OS manage expanding files? filling up blocks and what, linked lists or version thereof to track them?

FellippeHeitor

  • Guest
Re: REDIM _PRESERVE bug
« Reply #23 on: March 29, 2020, 01:19:26 pm »
REDIM the array and use _MEM to access its contents like it was a file.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #24 on: March 29, 2020, 01:39:09 pm »
Ya bplus - like a Random File but files tend to hold data long term whereas an array is just during a program run....more short term temporary hold of a value. Arrays, are all sequential store and sequential access. Not exactly sure what a Random Array would look like/ act like but, if it did exist, then resetting the records length may easier than a Redim _Preserve of an array.

Just read your recent post re Static/Dynamic arrays. I've tried to use Subs and Functions and reading Multi Dimensional arrays backwards to build a quasi Random Dynamic Array. What a mess in coding, hard to follow the action to find the glitches. Was fun but abandoned it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #25 on: March 29, 2020, 02:39:17 pm »
Ya bplus - like a Random File but files tend to hold data long term whereas an array is just during a program run....more short term temporary hold of a value. Arrays, are all sequential store and sequential access. Not exactly sure what a Random Array would look like/ act like but, if it did exist, then resetting the records length may easier than a Redim _Preserve of an array.

Just read your recent post re Static/Dynamic arrays. I've tried to use Subs and Functions and reading Multi Dimensional arrays backwards to build a quasi Random Dynamic Array. What a mess in coding, hard to follow the action to find the glitches. Was fun but abandoned it.

Arrays aren't accessed sequentially, just use index for the array "record" like get #file, recordNumber
dim records(lb to ub) as recordType
XPlace = records(recordNumber).x

Quote
I've tried to use Subs and Functions and reading Multi Dimensional arrays backwards to build a quasi Random Dynamic Array.
What? ;-)

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #26 on: March 30, 2020, 02:13:01 pm »
Alright, so no laughing. Here's what I was trying to do:

Imagine an Array like MainArray(a to z,1 to 12,1 to 31,1 to 50) ' I know this will exceed memory, bin there and found out, but just image it works.

So the first element is Years, 2nd is Months, 3rd is Days and 4th is 50 Events.

The program has multiple files which contain the data and MainArray is mostly used to Update the data. Now on a particular day you want to update Event 3 by 2, Event 25 by 6 and Event 18 by 4.
But the thing is those Events may be today's event or a past event, and chances are you know the event did occur but you don't have the specifics on when they occurred. The array needed a holding place in each of the year, month, and day for each of the events until the information of that event could be clarified. Redim _Preserve, then expand each element for a HOLD to place the data. If you should find a year for that data on the next run of the program, then Redim_Preseve, figure out if the year needs to be fitted in the middle of a to z or before or after a to z, and Swap the hell out of everything.

Sheeeze ...Be nice if there was just an automatic Random Access to a to z, whereby all that Redim_Preseve/Swapping is done.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #27 on: March 30, 2020, 03:12:24 pm »
@Dimster

I suspect you have over complicated a Database structure. YY-MM-DD is just a date field and if your events are just numbers within a range, 0 to 99 or 0 to 999... you could probably fit them all in a fixed length string that you could design a function to read or edit one of the 50 events with use of MID$
« Last Edit: March 30, 2020, 03:13:44 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: REDIM _PRESERVE bug
« Reply #28 on: March 30, 2020, 07:11:38 pm »
@Dimster

I suspect you have over complicated a Database structure. YY-MM-DD is just a date field and if your events are just numbers within a range, 0 to 99 or 0 to 999... you could probably fit them all in a fixed length string that you could design a function to read or edit one of the 50 events with use of MID$

Or else you need an index file to organize the data. The index might look something like:

YYYY-MM-DD-Event, Record-Number
2010-12-25-1, 1
2012-11-14-6, 2
2010-12-25-6, 3

The above would be an index pointing to the record position in the actual data file.  You wrote the data entry for your first event on Dec 25, 2012 in the first record spot.  The second event for that same date might be the 8th record.  The 6th event on that date is in the 3rd record position...

You create/write the data as needed, and the index is sortable for quick search and reference. 

If the data was of variable length, you'd do as above, but instead of Record Number, you'd track Start-Of-Data-Offset and End-Of-Data-Offset.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: REDIM _PRESERVE bug
« Reply #29 on: March 31, 2020, 09:30:51 am »
bplus - I agree combining the date fields to one YYYY-MM-DD field, stored as String, and info retrieved using Mid$ is an approach which I did consider but as I recall, there was a lot of coding for those times when I had only the month and event, or only the year and event, and sometimes just the event with lots of work to track down more specifics on the event. So I had to have the date field as only YYYY, or only MM, or only DD and the Event. Combining the date fields worked once I had all the data for a complete record. Also, the data in this array was used in calculations and stats, so it had to be converted from strings to numeric. I think I ran into some issues on the reliability of the numeric values which is why I settled on the date fields as numeric values at the outset rather than strings.

Steve - not exactly sure how the sortable index would work. The stat info needed would be say - how many times did event 3 occur on a Wednesday - or how often did event 6 and event 11 occur together in 2009. I see where the Updating would be a breeze, just add every thing to the last record and keep the array completely sequential. I was trying to find a logical place within the array to squeeze in the new data (thus the thought on Random access to the array), which made the calculations/stats more accurate if the data came to the formulas sequentially.

I did give up on that mighty array a number of years ago and only recently was trying to re-think it using some of the new, very rich QB64 syntax. I was looking at Redim _Preserve and it appears this works by making a copy of the entire array? I don't think it would say copy just a portion of an array? In the case of a my mighty array, if I wanted to make an update to say Feb 15, 2002, I could preserve all the data from the beginning of the array up to Feb 15, 2002 in one group, all the info in the array from Feb 16, 2002 in the next group - then write back the 1st group, then write my new entry for Feb 15, 2002 and then added the second group back? Redim _Preserve does all or nothing I gather???