It's to do with a() being a fixed-size array. It's not allocated memory and can't be changed.
It works if you make it dynamic though, e.g. dim a() or redim a(0 to 2).
I don't know what the best behaviour for a fixed array would be. But from brief tests, what happens is that on redim new memory is allocated, overriding the fixed-size data, and is deallocated at the end of the sub (or at least, an array of objects will be constructed and destroyed).
On redim preserve though, it seems to give an "out of memory" error. That would be a bug.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The compiler/runtime currently has no way to know whether the passed array is really dynamic or not. REDIM is only possible in case it's dynamic.
How about adding a flag to the array descriptors which the compiler creates for fixed-size arrays, and letting REDIM trigger a runtime error when given a fixed-size array? ERASE could perhaps also use this to change behaviour accordingly.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've just checked in QBasic, and code like this shows a "Duplicate definition" runtime error.
A flag sounds like a good idea. But a corresponding method to test the "fixedness" of an array would be useful too.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It's to do with
a()
being a fixed-size array. It's not allocated memory and can't be changed.It works if you make it dynamic though, e.g.
dim a()
orredim a(0 to 2)
.I don't know what the best behaviour for a fixed array would be. But from brief tests, what happens is that on
redim
new memory is allocated, overriding the fixed-size data, and is deallocated at the end of the sub (or at least, an array of objects will be constructed and destroyed).On
redim preserve
though, it seems to give an "out of memory" error. That would be a bug.The compiler/runtime currently has no way to know whether the passed array is really dynamic or not.
REDIM
is only possible in case it's dynamic.How about adding a flag to the array descriptors which the compiler creates for fixed-size arrays, and letting
REDIM
trigger a runtime error when given a fixed-size array?ERASE
could perhaps also use this to change behaviour accordingly.I've just checked in QBasic, and code like this shows a "Duplicate definition" runtime error.
A flag sounds like a good idea. But a corresponding method to test the "fixedness" of an array would be useful too.
Sorry, just found a related bug: in recent builds, keywords after
REDIM [PRESERVE]
- e.g.REDIM PRINT foo
are causing a segfault.This should fix the crash issue: [12f1e2]
Related
Commit: [12f1e2]
Fixed in fbc 1.08 [91b9f4df11f21d2056316d3836ea4ea00323f71d]
If compiled with '-e' option for run-time error checking, the
REDIM
will throw an error.If not compiled with '-e' then then
REDIM
silently fails and the array is not resizedRelated
Commit: [91b9f4]
Fixed - See also:
Github PR240 - Check for fixed length arrays when using ERASE & REDIM