Results 1 to 14 of 14

Thread: Getting the size of a dynamic string array in memory

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63

    Question Getting the size of a dynamic string array in memory

    I have a dynamic array of strings.

    Dim DynArray() As String
    Redim DynArray(x,y)

    How can I find the size of the array in memory?
    I have searched for information on the String data type and how it is stored in memory, but couldn't find much.

    If I had defined the size of the strings, this question would be easy. (x * y * SizeOfString) But my strings are of varying length.

    Is there any way to do this easily? Or do I have to keep track of the size of every string in the array using the Len() function?

    Any help greatly appreciated.

    Cedric

  2. #2
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    sizeof(arrayname)
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  3. #3
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Agh.. mybad. Too used to C++

    That doesn't exist in VB.

    I think you can use len(arrayname)
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63

    R

    sizeof() is a C function. It didn't work when I put it in my VB code.

    Is there some way to use it in VB?

    Cedric

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63

    r

    already tried Len()

  6. #6
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Hmm... there may be an API lemme check.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  7. #7
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    A quick way to do it?
    VB Code:
    1. Dim Temp As String
    2.  
    3. Temp = Join([i]mydynamicarray[/i])
    4. MsgBox Len(Temp)
    -Excalibur

  8. #8
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    This is far from optimal; its a bit of a cludge but it should work:


    If Dir("c:\temp.dat") <> "" Then Kill "c:\temp.dat"
    Open "c:\temp.dat" For Binary As #1
    Put #1, , arr
    Close #1
    sizeofarray = FileLen("c:\temp.dat")
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    interesting workaround with the temp file.
    I will try both methods.

    Thanks for your help.

    Cedric

  10. #10
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by ExcalibursZone
    A quick way to do it?
    VB Code:
    1. Dim Temp As String
    2.  
    3. Temp = Join([i]mydynamicarray[/i])
    4. MsgBox Len(Temp)
    I say take out the middle man!
    VB Code:
    1. MsgBox Len(Join(MyDynamicArray))
    You just proved that sig advertisements work.

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    I guess it wasn't clear that my array is two dimensional.

    Join() doesn't work with multidimensional arrays.

    I tried splitting up the array, but that's more work than it's worth.

    The temp file thing worked though.
    Thanks mlewis.

    Cedric

  12. #12
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Just to add a little extra on that: if you use the temp file and put, since the strings are variable length VB will first write an integer to the file that represents the length of the next string in characters, and then it will write the string itself. It will repeat that for every string, so the actual size of just the strings themselves will be filelength - (2 * total#ofstrings). That's not really much of a concern if you're only going for an estimate though.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  13. #13
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    This is true; but there is also some overhead for storing the array information, so the info written to the file counts as stuff used in RAM. Its just maybe 10-20 bytes off for huge arrays (40,000 elements or so)

    Note: don't quote me on this; just some quick & dirty estimates in my mind.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  14. #14
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    I just noticed that the original subject was the in memory size. I was thinking only of the string part itself. There's only a paltry amount . 20 bytes for just the array, plus 4 bytes per dimension. A 2D var length string array would be 28 + data size then. Also, in memory, the size of a string is 2x the number of characters, since VB strings are unicode (2 bytes per char). The file however is just 1 byte per char. I should have mentioned that before but it slipped my mind.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width