var-len??

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

var-len??

Post by Luis Babboni »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var-len??

Post by fxm »

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: 375
Joined: Mar 15, 2015 12:41

Re: var-len??

Post by Luis Babboni »

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

Re: var-len??

Post by jevans4949 »

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

Re: var-len??

Post by integer »

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
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: var-len??

Post by fxm »

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

Re: var-len??

Post by jevans4949 »

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: 252
Joined: Mar 12, 2006 16:25

Re: var-len??

Post by RockTheSchock »

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:
http://www.freebasic.net/forum/viewtopi ... ua#p195498

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

Re: var-len??

Post by RockTheSchock »

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