Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory used by array?

Status
Not open for further replies.

gmmastros

Programmer
Feb 15, 2005
14,902
US
I am trying to determine the amount of memory used by an array. Specifically if it's an array of user defined types containing strings. Using this code as an example...

Code:
Option Explicit

Private Type Person
    Id As Long
    EyeColor As String
    ShoeSize As Single
    Name As String
End Type

Private Sub Command1_Click()

    Dim People() As Person
    
    ReDim People(2)
    
    People(0).Id = 1
    People(0).Name = "Some Really long name"
    
    People(1).Id = 2
    People(1).Name = "Jo"
    
    People(2).Id = 3
    People(2).EyeColor = "Blue"
    People(2).ShoeSize = 9.5
    
    ' How do I determine the amount of memory used
    ' by the array?
    
End Sub



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I knew this had been answers before an it only took a half hour to find it but here it is thread222-1493764

As you will notice, it is not for a dynamic array of UDT's but using Len(MyUDT(0)) would work.



Good Luck

 
As described by strongm in the above-mentioned thread, that a UDT only stores pointers to variable length strings, no matter how big or small the strings are. And these string pointers are 4 bytes long.

As each member of the UDT Person is 4 bytes long, and the size of a Person variable turns out to be 16 bytes. There are 3 elements in the array, therefore total memory used by the array is 16 * 3 = 48 bytes.

In order to determine the total memory used by the array, you have to sum up the length of each string member in each element of the array.

Another method is to use fixed-length string variables in your array, but it has serious disadvantages. Fixed-length strings consume a fixed amount of memory, even if they are not used or initialized. And they cannot store data more than their size, so you have to be thoughtful when deciding their size.

Moreover, you might need to add a null terminator when assigning data to these fixed length strings, as the trailing characters are by default filled by spaces.

See thread711-763982 where we discussed the difference of having fixed and variable length strings as members of UDT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top