# Visual Basic > Visual Basic 6 and Earlier >  Large array allocation

## JAKLZ

Hello guys,

I'm new to the Forum, I apologize if I do something that is outside the standards of this forum.
I'm having trouble solving a problem of allocating an array with 10 dimensions. I've read on Microsoft's page, and I saw that it is possible to allocate up to 32 dimensions, forgive me if I'm wrong. My problem is that I need to allocate an array of type Boolean Ex:


code Code:
Dim Size as Long
 Size = 25
 ReDim MyArray(1 To Size - 9, 2 To Size - 8, 3 To Size - 7, 4 To Size - 6, 5 To Size - 5, 6 To Size - 4, 7 To Size - 3, 8 To Size - 2 , 9 To Size - 1, 10 To Size) As Boolean

This code is part of a much larger project, today I can allocate up to 6 dimensions in the same format and there is no problem with that, more so if it is from 7 to 10 which is what I need, from 'Memory Overflow'.
I wonder if anyone knows of a way around this, or is there another way to use it. Attached is a small sample of a much larger project, some .txt files are included for testing purposes. The program is basically for calculating lottery wheel coverage, so far the code works very well and very fast, but I need it to meet all the conditions of a wheel.

Sample Project :
Projeto.zip

----------


## Elroy

Oh goodness, a Boolean is actually (under the hood) just a special case of an Integer (two bytes, 16 bits), and you're only using it as a flag.  When I need a *bunch* of these flags, I always start thinking about bit manipulation, and abandon the Boolean type.

You could develop a class (or even just a function) that "looked like" an array, but was _much_ more memory efficient than using Booleans.

EDIT:  Yeah, I took a look at your project, and you're blowing up memory.

I just took one of your files, and V=25, which means that each of the 7-dimension array wanted about 18 elements.  So, the memory you requested was:

18^7 * 2
18 items per dim,
7 dimensions
2 bytes per Boolean

That results in 1,224,440,064.  Running in the IDE, that's too much.  And compiled, it's still really too much.

You can divide that number by 16 if you used bits instead of Booleans.

Here's the line that blew up on me:



```

ReDim theTriplets(1 To V - 6, 2 To V - 5, 3 To V - 4, 4 To V - 3, 5 To V - 2, 6 To V - 1, 7 To V) As Boolean

```

----------


## JAKLZ

Thanks for the quick response.




> You could develop a class (or even just a function) that "looked like" an array, but was much more memory efficient than using Booleans.


Using your analogy of using a class as an alternative, I don't know if I missed something, but in terms of efficiency the posted code runs very fast. And all the tests I did with Dictionaries, and Collections I didn't get anywhere near the execution speed. But I have no idea how to solve the problem using a class, I've tried several ways to run it, I even have other codes that run and execute the same process, but much much slower than this one.

----------


## Elroy

> Thanks for the quick response.
> 
> Using your analogy of using a class as an alternative, I don't know if I missed something, but in terms of efficiency the posted code runs very fast. And all the tests I did with Dictionaries, and Collections I didn't get anywhere near the execution speed. But I have no idea how to solve the problem using a class, I've tried several ways to run it, I even have other codes that run and execute the same process, but much much slower than this one.


Just write functions in a BAS module to do it.  Bit manipulation is very fast.  Let me see if I can find an example as a starting place.  Can't quickly find anything.  I'll write a function as a starting place.




```

Public Sub BitOn(ByRef iTarget As Integer, bbTheBit1to16 As Byte)
    Dim iMask As Byte
    Select Case bbTheBit1to16
    Case 1:     iMask = &H1
    Case 2:     iMask = &H2
    Case 3:     iMask = &H4
    Case 4:     iMask = &H8
    Case 5:     iMask = &H10
    Case 6:     iMask = &H20
    Case 7:     iMask = &H40
    Case 8:     iMask = &H80
    Case 9:     iMask = &H100
    Case 10:    iMask = &H200
    Case 11:    iMask = &H400
    Case 12:    iMask = &H800
    Case 13:    iMask = &H1000
    Case 14:    iMask = &H2000
    Case 15:    iMask = &H4000
    Case 16:    iMask = &H8000
    End Select   
    '
    iTarget = iTarget Or iMask
End Sub

Public Sub BitOff(ByRef iTarget As Integer, bbTheBit1to16 As Byte)
    Dim iMask As Byte
    Select Case bbTheBit1to16
    Case 1:     iMask = &HFFFE
    Case 2:     iMask = &HFFFD
    Case 3:     iMask = &HFFFB
    Case 4:     iMask = &HFFF7
    Case 5:     iMask = &HFFEF
    Case 6:     iMask = &HFFDF
    Case 7:     iMask = &HFFBF
    Case 8:     iMask = &HFF7F
    Case 9:     iMask = &HFEFF
    Case 10:    iMask = &HFDFF
    Case 11:    iMask = &HFBFF
    Case 12:    iMask = &HF7FF
    Case 13:    iMask = &HEFFF
    Case 14:    iMask = &HDFFF
    Case 15:    iMask = &HBFFF
    Case 16:    iMask = &H7FFF
    End Select
    '
    iTarget = iTarget And iMask
End Sub


```

Now, those two functions allow you to toggle individual bits of an Integer type, getting 16 flags out of a single Integer.  And that Integer can come from an array or wherever you need it to come from.  Here's a third function to test those bits.



```

Public Function BitIsOn(ByRef iTarget As Integer, bbTheBit1to16 As Byte) As Boolean
    Dim iMask As Byte
    Select Case bbTheBit1to16
    Case 1:     iMask = &H1
    Case 2:     iMask = &H2
    Case 3:     iMask = &H4
    Case 4:     iMask = &H8
    Case 5:     iMask = &H10
    Case 6:     iMask = &H20
    Case 7:     iMask = &H40
    Case 8:     iMask = &H80
    Case 9:     iMask = &H100
    Case 10:    iMask = &H200
    Case 11:    iMask = &H400
    Case 12:    iMask = &H800
    Case 13:    iMask = &H1000
    Case 14:    iMask = &H2000
    Case 15:    iMask = &H4000
    Case 16:    iMask = &H8000
    End Select
    '
    BitIsOn = iTarget And iMask
End Function


```

I hope that gets you started.

ADDED:  Ohhh, and I did the first two as Sub procedures (rather than Functions).  I did this to gain a bit of speed.  The iTarget will just be returned from either Sub with the bit you've specified toggled.

ADDED2:  I initially forgot my "End Select" statements, so re-grab it if you grabbed it fast.

----------


## Elroy

Also, just thinking about this, here's another alternative, which wouldn't solve the inefficiency problem, but probably would get you going.

That's a procedure for allowing you to create arrays in "far memory" (outside of the 2GB you're allowed by VB6).  With that procedure, you can access as much memory as is in your computer (and maybe more, considering virtual memory).

----------


## JAKLZ

Ok, very grateful for the direction of something, I've been trying something for weeks and without success. Now I have something else to interact with.

----------


## baka

if the last dimension is within range 1-16 u can use integer, even if u dont use all 16 flags that way, u skip 1 dimension
if u instead have more, u can increase to LONG, that gives 32 bits and if thats not enough u can increase with currency for 64bits.

but if the dimension is larger than what even using bits can handle, better with a collection.
so u only "store" used, while not used are not stored.

----------


## baka

here a very an example of how u can do. u can have 20 dimensions if u want. 



```
Const Dimension     As Integer = 10

Enum mlType
    GetBool
    SetTrue
    SetFalse
    Clear
End Enum

Function mlArray(ByVal Command As mlType, ParamArray args()) As Boolean
    Static mData()  As String * Dimension
    Static mCount   As Long
    Dim Id          As String
    Dim i           As Long
    
    If Command = Clear Then
        Erase mData()
        mCount = 0
        Exit Function
    End If
    
    For i = 0 To UBound(args)
        Id = Id & Chr(args(i))
    Next i
    
    While Len(Id) < Dimension
        Id = Id & Chr(255)
    Wend
    
    For i = 1 To mCount
        If mData(i) = Id Then
            mlArray = True
            If Command = SetFalse Then
                If i < mCount Then mData(i) = mData(mCount)
                mCount = mCount - 1
                If mCount = 0 Then Erase mData Else ReDim Preserve mData(1 To mCount)
            End If
            Exit Function
        End If
    Next i
    
    If Command = SetTrue Then
        mCount = mCount + 1
        ReDim Preserve mData(1 To mCount)
        mData(mCount) = Id
    End If
End Function
```

----------


## JAKLZ

Well we already have some code samples, I'm grateful for that. More actually taking my code example as a base, what I'm trying to do is create a code as fast as what's in the sample that is possible to count how many combinations, or how many bits, are equal, based on a sample of combinations, and perform the conference in a larger matrix, to verify how many are invalid.

Here a brief explanation:
http://www2.arnes.si/~ubolti/ininuga/programming.htm

----------

