Bug when redimensioning multi-dimension arrays? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Bug when redimensioning multi-dimension arrays? (/showthread.php?tid=2813) |
RE: Bug when redimensioning multi-dimension arrays? - SMcNeill - 06-21-2024 (06-20-2024, 09:16 PM)12centuries Wrote: One question I have: how does QB64 store the LBOUNDS and UBOUNDS for an array? Could you point me to the data structure in source? The data structure is just an array. The bounds are stored in some of the array entries. From our source in libqb.cpp: Code: (Select All) ptrszint func_lbound(ptrszint *array, int32 index, int32 num_indexes) { RE: Bug when redimensioning multi-dimension arrays? - bplus - 06-21-2024 (06-20-2024, 09:52 PM)12centuries Wrote: WOW! I walked away for a bit and found a flurry of solutions. i recommend you put the x in first position and y in second, (x, y) all graphics commands work that way. the only thing that doesn't is locate row, column which is why i am in habit or saying row, column for locate. easy to remember alphabetically x comes before y. RE: Bug when redimensioning multi-dimension arrays? - DSMan195276 - 06-21-2024 (06-20-2024, 09:16 PM)12centuries Wrote: It seems that QB wants to keep all of the data in the array and just change the indexes that point to the data. Is that true? I'm not sure I understand the reasoning behind that, but I'd love to know.In a lot of ways it's just laziness, it's the easiest way to implement _preserve because it requires no special copying logic. The data backing an array is always stored as one big one-dimensional array, even for multi-dimensional arrays, and that backing data is simply copied verbatim into the memory for the resized array. When the array is accessed, QB64 does logic like `x * bound1 + y * bound2 + z` to calculation the location of the `x,y,z` index into that backing one-dimensional array. If the size of `bound1` or `bound2` are changed then that throws off all the math for how the `x,y,z` indexes correspond to the backing data, even though the backing data was never changed. That said, from a technical sense _preserve could maintain the index locations, it's just more complicated to implement. RE: Bug when redimensioning multi-dimension arrays? - luke - 06-21-2024 It's an implementation detail and not accessible from within BASIC code, but the array descriptor data structure is thus: Code: (Select All) a[0]: pointer to data RE: Bug when redimensioning multi-dimension arrays? - bobalooie - 06-21-2024 (06-20-2024, 02:07 AM)SMcNeill Wrote:(06-19-2024, 10:36 PM)TerryRitchie Wrote: Yep, this a known issue. I'll let Steve explain it in detail though. He explained it to me a few years back when I was wondering the same thing but I can't seem to find that explanation now? It may have been on a previous forum. Perhaps we need a a keyword such as _ERASE(Temp) that cleans the array out memory completely. I remember having that keyword back in 8-bit MS Basic. RE: Bug when redimensioning multi-dimension arrays? - 12centuries - 06-21-2024 (06-20-2024, 10:57 PM)Kernelpanic Wrote: The manual for Quick- and QBasic: "Neither the type nor the dimension of an array may be changed."Quite right, QBasic and QuickBasic were much more limited than even PDS/QBX were, which allowed the size of dimensions to be changed: "Although you can change the size of an array's dimensions with the REDIM statement, you can not change the number of dimensions." (Microsoft BASIC Professional Development System 7.1 — Basic Language Reference, 1989, p. 289). So many languages have the ability to change array dimensions, like python's del statement, javascript's slice method, the Arrays.copyOf method in Java, or std::vector's resize method in C++. It's too bad Q[uick]Basic didn't have something. I think the _PRESERVE keyword is a credit to the QB64 developers for including it. I suppose that warning was a bit obvious, It's a long-ingrained habit of mine to ensure that any potential loss of data is fully documented so that anyone who reuses my code would have a full grasp of the consequences. So here's what I'm doing...this code is part of a game with multiple levels that are loaded from user-editable text files. The dimensions of the maps are not known until read, and rather than read through the file twice (first a parsing read, then array dimensioning, then a loading read), I opted to use a more adaptive approach which adjusts the array size as it loads. When all is done, the array is sometimes larger than required, so I reduce it. While it's not intended to pare down existing data from an array, you could use it for that, so I think the warning is warranted. RE: Bug when redimensioning multi-dimension arrays? - Kernelpanic - 06-21-2024 @12centuries - I have checked again now, there is no resizing in C either - but in C you can program almost everything yourself if you can do it. Does it make sense? Regarding "std::vector's resize", the dimension is not changed, or am I misunderstanding that? Only the size of the array is changed. This also works with Basic64. Code: (Select All)
|