Page 1 of 1
Preload global arrays with memory
Posted: Sun Feb 10, 2013 11:38 am
by Michael Vogel
When I write programs like the
rotating world, I have to decide, where to take the data (numeric, images sound etc.) from. It can be loaded from external files or must be kept twice in memory (data section and loaded dynamically).
Because of some issues when using external files (security etc.), using DataSection could be useful. But what about assigning a (global) array to a memory location which has been defined by data? If possible, this would speed up program execution (no need of filling the array with values) and reduces memory hunger.
I'm not sure, if this could be realized, and, of course, wrong array definitions could lead to unwanted results, but that shouldn't be a big problem.
Code: Select all
Global Dim Test.w(10000) Using ?TestMemory
DataSection
TestMemory:
Data.w 9,2,3,34,1,5,6,6,7,...
EndDataSection
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 1:14 pm
by spikey
That would be a pointer? Unless you propose to copy the data into the array in which case you don't avoid the extra memory overhead.
Code: Select all
Define *Ix
*Ix = ?TestMemory
; Indexing from 0.
; Word is two bytes so multiply all indices by 2.
Debug PeekW(*Ix)
Debug PeekW(*Ix + (2 * 2))
Debug PeekW(*Ix + (5 * 2))
DataSection
TestMemory:
Data.w 9,2,3,34,1,5,6,6,7
EndDataSection
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 1:24 pm
by Little John
@spikey:
Something like that can be done even with an array (in a structure).
Code: Select all
DataSection
TestMemory:
Data.w 9,2,3,34,1,5,6,6,7
EndDataSection
Structure DataArray
foo.w[0]
EndStructure
Define i.i, *test.DataArray = ?TestMemory
For i = 0 To 8
Debug *test\foo[i]
Next
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 4:21 pm
by Demivec
Michael Vogel wrote:When I write programs like the
rotating world, I have to decide, where to take the data (numeric, images sound etc.) from. It can be loaded from external files or must be kept twice in memory (data section and loaded dynamically).
Because of some issues when using external files (security etc.), using DataSection could be useful. But what about assigning a (global) array to a memory location which has been defined by data? If possible, this would speed up program execution (no need of filling the array with values) and reduces memory hunger.
I'm not sure, if this could be realized, and, of course, wrong array definitions could lead to unwanted results, but that shouldn't be a big problem.
Code: Select all
Global Dim Test.w(10000) Using ?TestMemory
DataSection
TestMemory:
Data.w 9,2,3,34,1,5,6,6,7,...
EndDataSection
You can do this with a pointer to a static array.
Code: Select all
Structure TestMemoryArray
w.w[9]
EndStructure
Global *tma.TestMemoryArray = ?TestMemory ;Dim Test.w(10000) Using ?TestMemory
DataSection
TestMemory:
Data.w 9,2,3,34,1,5,6,6,7 ;,...
EndDataSection
Define i
For i = 0 To 8
Debug *tma\w[i]
Next
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 4:26 pm
by Little John
Demivec wrote:You can do this with a pointer to a static array.
[...]
That's exactly what I wrote.

Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 5:00 pm
by Demivec
Little John wrote:Demivec wrote:You can do this with a pointer to a static array.
[...]
That's exactly what I wrote.

Well, that isn't exactly what you wrote, which is why I made my post.
Our codes are almost the same but they differ in one respect. Yours doesn't dimension the array, mine does.
That means yours won't have its bounds checked and that it has some flexibility so that it can be reused for different things, while with mine it has defined bounds and very limited flexibility.

.
I made the post because I thought it was a small but important difference to show.
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 5:16 pm
by ts-soft
The code by Demivec is more sure, gives a IMA outside Array.
Code by Little John shows random values outside Array.
Re: Preload global arrays with memory
Posted: Sun Feb 10, 2013 6:02 pm
by Little John
Demivec wrote:Well, that isn't exactly what you wrote
Sorry, "exactly what I wrote" was wrong of course.
I just didn't see a significant difference, because I thought that the bounds of a static array in a structure were
never checked. Please don't ask me why I believed that.
Demivec wrote:because I thought it was a small but important difference to show.
I agree. Thanks.