var-len??

New to FreeBASIC? Post your questions here.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

var-len??

Postby Luis Babboni » May 23, 2015 13:10

Hi,

I tried to define this matrix:

Dim MaSob (1 To 12, 1 To 12, 1 To 12, 1 To 64, 1 To 63, 1 To 62) As Integer<32>

And the compiler warning me in blue saying:
"Array too large for stack, consider making it var-len or SHARED"

What is "var-len"?
I could not make a relation between what I understand "Shared" means and this suggestion. :-/

I searching in the Help but did not helps me :-/

Thnks.
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var-len??

Postby fxm » May 23, 2015 13:43

Your array (defined as local) is to big to be put on the stack (default size = 1024 KBytes).

So you have two possibilities for solving that problem:
- either declare the array as gobal (and not local), by using SHARED:

Code: Select all

Dim Shared MaSob (1 To 12, 1 To 12, 1 To 12, 1 To 64, 1 To 63, 1 To 62) As Integer<32>
- or declare the array as dynamic of var-len (variable length), by using REDIM:

Code: Select all

Dim MaSob () As Integer<32>  '' or Dim Shared MaSob () As Integer<32>
Redim MaSob (1 To 12, 1 To 12, 1 To 12, 1 To 64, 1 To 63, 1 To 62)

There are also some others variants to these above syntaxes.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: var-len??

Postby Luis Babboni » May 23, 2015 18:14

As always... Thanks fxm! :-)
jevans4949
Posts: 1148
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: var-len??

Postby jevans4949 » May 25, 2015 3:28

I make that 1.6 terabytes of array ... how big is your RAM?
integer
Posts: 386
Joined: Feb 01, 2007 16:54
Location: usa

Re: var-len??

Postby integer » May 25, 2015 22:30

jevans4949 wrote:I make that 1.6 terabytes of array ... how big is your RAM?

12 * 12 * 12 * 64 * 63 * 62 is about 432 million

at 4 bytes / Integer requires about 1.7 billion bytes or 1.7 gigabytes

Oh! This is the proof that jevans4949 lives in Washington D.C.
Or perhaps your terabyte is smaller than my terabyte.
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var-len??

Postby fxm » May 26, 2015 4:54

Yes, 1.6 GB (12*12*12*64*63*62*4/1024/1024/1024).
jevans4949
Posts: 1148
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: var-len??

Postby jevans4949 » May 26, 2015 23:31

OK, I did the calculationbefre posting, and yes it comes to 1.6-and-a-bit Terabyes; I decided 1.6 was good enough to point up the problem for sanyone with an avearge PC.

@Luis Babboni: You probably need to think about a sparse array. The principle with that is that the top 5 levels of the array are 1-dimensional arrays of pointers, and the bottom level is a 1-dimensional array of integers. Initially, only the top level array exists, and all its pointers are null.

When you need to store a new value for MaSob(i,j,k,l,m,n) check if the ith pointer in the top array is still null; if it is, allocate a new 2nd-level array and set the ith element in the top array to point to it. Proceed similarly with levels 2 thru 5. When you get to level 6, if the integer array doesn't exist, then allocate the array and store your value (initialising the rest of the integers to zero, or some "impossible" value to indicate they are uninitialised).

When you need to retrieve the value of an element in the array, if any of the pointers in the chain are null, then the element is effectively not initialised (or may be taken as zero, if that is how your program works).
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: var-len??

Postby RockTheSchock » May 27, 2015 6:37

Maybe you can use lua to manage your sparse arrays. You would only need to implement methods for reading / writing / loading / saving. Lua maybe uses about twice the memory as an optimised freebasic version would use, but thats better than 1.6TB

Reading a lua table:
viewtopic.php?f=7&t=22223&hilit=+Lua#p195498

Use serpent to serialize lua (make a dump / load and store)
http://notebook.kulchenko.com/programmi ... ty-printer
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: var-len??

Postby RockTheSchock » May 27, 2015 7:19

jevans4949 wrote:@Luis Babboni: You probably need to think about a sparse array. The principle with that is that the top 5 levels of the array are 1-dimensional arrays of pointers, and the bottom level is a 1-dimensional array of integers. Initially, only the top level array exists, and all its pointers are null.


Well to implement a sparse array efficiently, you need to know the data/memory layout used later. If there are just 2000 values used, read and written only sporadically, than you can store the sparse table as array with single linked lists and iterate through them.

If you want faster read access with a lot more values than you can store the sparse table as array with sorted double linked lists. Inserting values is a bit slower but you can use than a binary search to access single values even for several several ten thousands values fast enough ( What is fast enough?)

Most languages implement sparse associative arrays with hashtables like php or lua.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests