allocating_20arrays_20using_20the_20windows_20api
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
allocating_20arrays_20using_20the_20windows_20api [2018/03/31 13:19] – external edit 127.0.0.1 | allocating_20arrays_20using_20the_20windows_20api [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | =====Allocating arrays using the Windows API===== | + | =====Allocating arrays using the Windows |
//by Richard Russell, December 2006//\\ \\ You can allocate arrays, using **DIM**, either on BASIC' | //by Richard Russell, December 2006//\\ \\ You can allocate arrays, using **DIM**, either on BASIC' | ||
+ | <code bb4w> | ||
DIM globalarray1(...), | DIM globalarray1(...), | ||
REPEAT | REPEAT | ||
Line 20: | Line 21: | ||
REM ..... | REM ..... | ||
ENDPROC | ENDPROC | ||
+ | </ | ||
\\ Here the memory occupied by arrays **localarray1()** and **localarray2()** is freed on exit from the first procedure and can be reused by **localarray3()** and **localarray4()** in the second procedure. This is a flexible arrangement and some combination of global and local arrays will usually suffice.\\ \\ However there may very rarely be situations which cannot satisfactorily be resolved using the capabilities of **DIM**. One is when the size of an array exceeds the amount of memory available to BBC BASIC for Windows (a total of 256 Megabytes). Another is when the memory occupied by the arrays cannot be allocated and freed in the required sequence, for example:\\ \\ | \\ Here the memory occupied by arrays **localarray1()** and **localarray2()** is freed on exit from the first procedure and can be reused by **localarray3()** and **localarray4()** in the second procedure. This is a flexible arrangement and some combination of global and local arrays will usually suffice.\\ \\ However there may very rarely be situations which cannot satisfactorily be resolved using the capabilities of **DIM**. One is when the size of an array exceeds the amount of memory available to BBC BASIC for Windows (a total of 256 Megabytes). Another is when the memory occupied by the arrays cannot be allocated and freed in the required sequence, for example:\\ \\ | ||
Line 26: | Line 28: | ||
- Destroy first array | - Destroy first array | ||
- Destroy second array | - Destroy second array | ||
- | \\ Because LOCAL arrays are allocated on the //stack// they can only be destroyed in the reverse order from which they were created.\\ \\ In these cases it is possible to utilise the capabilities of the **Windows API** to allocate and free the memory used by arrays. By setting BASIC' | + | \\ Because LOCAL arrays are allocated on the //stack// they can only be destroyed in the reverse order from which they were created.\\ \\ In these cases it is possible to utilise the capabilities of the **Windows |
+ | |||
+ | **BBC BASIC for Windows: | ||
+ | <code bb4w> | ||
DEF PROCdim1d(RETURN A(),S%,D1%) | DEF PROCdim1d(RETURN A(),S%,D1%) | ||
LOCAL A% | LOCAL A% | ||
Line 45: | Line 50: | ||
!^A() = 0 | !^A() = 0 | ||
ENDPROC | ENDPROC | ||
+ | </ | ||
+ | |||
+ | **BBC BASIC for SDL 2.0:** | ||
+ | <code bb4w> | ||
+ | DEF PROCdim1d(RETURN A(),S%,D1%) | ||
+ | LOCAL a%% | ||
+ | SYS " | ||
+ | IF @platform% AND &40 ELSE a%% = !^a%% | ||
+ | ?a%%=1 : a%%!1=D1%+1 | ||
+ | PTR(A()) = a%% | ||
+ | ENDPROC | ||
+ | |||
+ | DEF PROCdim2d(RETURN A(), | ||
+ | LOCAL a%% | ||
+ | SYS " | ||
+ | IF @platform% AND &40 ELSE a%% = !^a%% | ||
+ | ?a%%=2 : a%%!1=D1%+1 : a%%!5=D2%+1 | ||
+ | PTR(A()) = a%% | ||
+ | ENDPROC | ||
+ | |||
+ | DEF PROCundim(RETURN A()) | ||
+ | SYS " | ||
+ | PTR(A()) = 0 | ||
+ | ENDPROC | ||
+ | </ | ||
+ | |||
In each case the parameters supplied to the **PROCdim** procedure are the name of the array to be created, the size (in bytes) of each array element and the array' | In each case the parameters supplied to the **PROCdim** procedure are the name of the array to be created, the size (in bytes) of each array element and the array' | ||
Line 56: | Line 87: | ||
| **80-bit float** e.g. array()\\ | size=**10**+\\ | | | **80-bit float** e.g. array()\\ | size=**10**+\\ | | ||
+ BB4W version 6 only\\ \\ Note that if you free a **string** array using **PROCundim** you must first empty all the elements (set them to zero-length strings), for example:\\ \\ | + BB4W version 6 only\\ \\ Note that if you free a **string** array using **PROCundim** you must first empty all the elements (set them to zero-length strings), for example:\\ \\ | ||
+ | <code bb4w> | ||
array$() = "" | array$() = "" | ||
PROCundim(array$()) | PROCundim(array$()) | ||
+ | </ | ||
You could use the above procedures to allocate and free arrays in a sequence which cannot otherwise be achieved:\\ \\ | You could use the above procedures to allocate and free arrays in a sequence which cannot otherwise be achieved:\\ \\ | ||
+ | <code bb4w> | ||
PROCdim1d(arr1%(), | PROCdim1d(arr1%(), | ||
arr1%(50) = 12345678 | arr1%(50) = 12345678 | ||
Line 67: | Line 101: | ||
PRINT arr2%(99) | PRINT arr2%(99) | ||
PROCundim(arr2%()) | PROCundim(arr2%()) | ||
+ | </ |
allocating_20arrays_20using_20the_20windows_20api.1522502345.txt.gz · Last modified: 2024/01/05 00:18 (external edit)