Preload global arrays with memory

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Preload global arrays with memory

Post 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
User avatar
spikey
Enthusiast
Enthusiast
Posts: 751
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Preload global arrays with memory

Post 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
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Preload global arrays with memory

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Preload global arrays with memory

Post 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
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Preload global arrays with memory

Post by Little John »

Demivec wrote:You can do this with a pointer to a static array.
[...]
That's exactly what I wrote. :-)
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Preload global arrays with memory

Post 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. :wink:

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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Preload global arrays with memory

Post by ts-soft »

The code by Demivec is more sure, gives a IMA outside Array.
Code by Little John shows random values outside Array.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Preload global arrays with memory

Post 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.
Post Reply